| 1 | package Yote::YapiServer::App::Base; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base 'Yote::YapiServer::BaseObj'; | ||
| 6 | |||
| 7 | # Database column definitions - subclasses can add more | ||
| 8 | our %cols = ( | ||
| 9 | app_name => 'VARCHAR(64)', | ||
| 10 | created => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP', | ||
| 11 | description => 'VARCHAR(64)', | ||
| 12 | settings => '*HASH<64>_Text', | ||
| 13 | app_version => 'INTEGER', | ||
| 14 | ); | ||
| 15 | |||
| 16 | # Class-level app version. Subclasses override this to | ||
| 17 | # indicate a new version of their app schema/logic. | ||
| 18 | our $app_version = 1; | ||
| 19 | |||
| 20 | # Method access control - subclasses should override | ||
| 21 | # Format: method_name => { public => 1, auth => 1, owner_only => 1, admin_only => 1 } | ||
| 22 | our %METHODS = (); | ||
| 23 | |||
| 24 | # Field visibility - subclasses should override | ||
| 25 | our %FIELD_ACCESS = ( | ||
| 26 | app_name => { public => 1 }, | ||
| 27 | created => { public => 1 }, | ||
| 28 | settings => { admin_only => 1 }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | # Public vars exposed on connect - subclasses should override | ||
| 32 | our %PUBLIC_VARS = (); | ||
| 33 | |||
| 34 | #---------------------------------------------------------------------- | ||
| 35 | # App-specific accessor (PUBLIC_VARS is app-only, not in BaseObj) | ||
| 36 | #---------------------------------------------------------------------- | ||
| 37 | |||
| 38 | sub public_vars { | ||
| 39 | my ($self) = @_; | ||
| 40 | my $class = ref($self) || $self; | ||
| 41 | no strict 'refs'; | ||
| 42 | return \%{"${class}::PUBLIC_VARS"}; | ||
| 43 | } | ||
| 44 | |||
| 45 | # Returns the class-level app version (from $app_version package variable). | ||
| 46 | # Subclasses override $app_version to bump their version. | ||
| 47 | sub version { | ||
| 48 | my $class = ref($_[0]) || $_[0]; | ||
| 49 | no strict 'refs'; | ||
| 50 | return ${"${class}::app_version"} // 1; | ||
| 51 | } | ||
| 52 | |||
| 53 | #---------------------------------------------------------------------- | ||
| 54 | # Method authorization | ||
| 55 | #---------------------------------------------------------------------- | ||
| 56 | |||
| 57 | sub authorize_method { | ||
| 58 | my ($self, $method, $session, $user) = @_; | ||
| 59 | |||
| 60 | my $methods = $self->method_defs; | ||
| 61 | my $method_def = $methods->{$method}; | ||
| 62 | |||
| 63 | return (0, "unknown method: $method") unless $method_def; | ||
| 64 | |||
| 65 | # Public methods - no auth required | ||
| 66 | return (1) if $method_def->{public}; | ||
| 67 | |||
| 68 | # All other methods require authentication | ||
| 69 | return (0, "authentication required") unless $session && $user; | ||
| 70 | |||
| 71 | # Admin-only methods | ||
| 72 | if ($method_def->{admin_only}) { | ||
| 73 | return (0, "admin access required") unless $user->get_is_admin; | ||
| 74 | } | ||
| 75 | |||
| 76 | return (1); | ||
| 77 | } | ||
| 78 | |||
| 79 | #---------------------------------------------------------------------- | ||
| 80 | # Object-level authorization (for methods called on specific objects) | ||
| 81 | #---------------------------------------------------------------------- | ||
| 82 | |||
| 83 | sub authorize_object_method { | ||
| 84 | my ($self, $target_obj, $method, $session, $user) = @_; | ||
| 85 | |||
| 86 | # First check if object is in session's exposed set | ||
| 87 | return (0, "object not accessible") unless $session->can_access($target_obj); | ||
| 88 | |||
| 89 | # Get method definition from the target object's class | ||
| 90 | my $methods = $target_obj->method_defs; | ||
| 91 | my $method_def = $methods->{$method}; | ||
| 92 | |||
| 93 | return (0, "unknown method: $method") unless $method_def; | ||
| 94 | return (1) if $method_def->{public}; | ||
| 95 | return (0, "authentication required") unless $session && $user; | ||
| 96 | |||
| 97 | # Owner-only: object must belong to calling user | ||
| 98 | if ($method_def->{owner_only}) { | ||
| 99 | if ($target_obj->can('get_owner')) { | ||
| 100 | my $owner = $target_obj->get_owner; | ||
| 101 | return (0, "owner access required") | ||
| 102 | unless $owner && $owner->id eq $user->id; | ||
| 103 | } elsif ($target_obj->isa('Yote::YapiServer::User')) { | ||
| 104 | # User objects - owner is self | ||
| 105 | return (0, "owner access required") | ||
| 106 | unless $target_obj->id eq $user->id; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | # Admin-only | ||
| 111 | if ($method_def->{admin_only}) { | ||
| 112 | return (0, "admin access required") unless $user->get_is_admin; | ||
| 113 | } | ||
| 114 | |||
| 115 | return (1); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | #---------------------------------------------------------------------- | ||
| 120 | # Connection metadata (what client receives on connect) | ||
| 121 | #---------------------------------------------------------------------- | ||
| 122 | |||
| 123 | sub connect_info_methods { | ||
| 124 | my ($self, $session, $user) = @_; | ||
| 125 | |||
| 126 | my $methods = $self->method_defs; | ||
| 127 | my @available_methods; | ||
| 128 | |||
| 129 | for my $method (sort keys %$methods) { | ||
| 130 | my $def = $methods->{$method}; | ||
| 131 | |||
| 132 | # Include public methods always | ||
| 133 | if ($def->{public}) { | ||
| 134 | push @available_methods, $method; | ||
| 135 | next; | ||
| 136 | } | ||
| 137 | |||
| 138 | # Include auth methods if user is logged in | ||
| 139 | if ($user && ($def->{auth} || $def->{owner_only})) { | ||
| 140 | push @available_methods, $method; | ||
| 141 | next; | ||
| 142 | } | ||
| 143 | |||
| 144 | # Include admin methods if user is admin | ||
| 145 | if ($user && $user->get_is_admin && $def->{admin_only}) { | ||
| 146 | push @available_methods, $method; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | return \@available_methods; | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; | ||
| 154 | |||
| 155 | __END__ |