| 1 | package Yote::YapiServer::App::Base; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use base 'Yote::YapiServer::BaseObj'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | # Database column definitions - subclasses can add more | ||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | 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 | 2 | 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 | 2 | our %METHODS = (); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # Field visibility - subclasses should override | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | 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 | 2 | our %PUBLIC_VARS = (); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # App-specific accessor (PUBLIC_VARS is app-only, not in BaseObj) | ||||||||||||||||||||||||||||||||||||||||||
| 36 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | sub public_vars { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
public_vars: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | 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 | 3 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
version: called CC=3 SCAR=11.2
Pod
undocumented
Condition66%2/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | ▶ | return ${"${class}::app_version"} // 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
${"${class}::app_version";} // 1
MC/DC0%0/1
${"${class}::app_version";} // 1
${"${class}::app_version";}
Truth table50%1/2
A
A= ${"${class}::app_version";}
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # Method authorization | ||||||||||||||||||||||||||||||||||||||||||
| 55 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | sub authorize_method { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 12 | ▶ | my ($self, $method, $session, $user) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
authorize_method: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 12 | my $methods = $self->method_defs; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 12 | my $method_def = $methods->{$method}; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 12 | ▶ | return (0, "unknown method: $method") unless $method_def; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # Public methods - no auth required | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 11 | ▶ | return (1) if $method_def->{public}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # All other methods require authentication | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | ▶ | return (0, "authentication required") unless $session && $user; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$session and $user
MC/DC100%2/2
$session and $user
$session
$user
Truth table100%3/3
A and B
A= $sessionB= $user
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # Admin-only methods | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | if ($method_def->{admin_only}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | return (0, "admin access required") unless $user->get_is_admin; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | return (1); | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # Object-level authorization (for methods called on specific objects) | ||||||||||||||||||||||||||||||||||||||||||
| 81 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | sub authorize_object_method { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 5 | ▶ | my ($self, $target_obj, $method, $session, $user) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
authorize_object_method: called CC=14 SCAR=31.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # First check if object is in session's exposed set | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 5 | ▶ | return (0, "object not accessible") unless $session->can_access($target_obj); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # Get method definition from the target object's class | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | my $methods = $target_obj->method_defs; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | my $method_def = $methods->{$method}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | return (0, "unknown method: $method") unless $method_def; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | return (1) if $method_def->{public}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | return (0, "authentication required") unless $session && $user; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$session and $user
MC/DC0%0/2
$session and $user
$session
$user
Truth table33%1/3
A and B
A= $sessionB= $user
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | # Owner-only: object must belong to calling user | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | if ($method_def->{owner_only}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | if ($target_obj->can('get_owner')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | my $owner = $target_obj->get_owner; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | return (0, "owner access required") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$owner and $owner->id eq $user->id
MC/DC50%1/2
$owner and $owner->id eq $user->id
$owner
$owner->id eq $user->id
Truth table66%2/3
A and B
A= $ownerB= $owner->id eq $user->id
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | unless $owner && $owner->id eq $user->id; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } elsif ($target_obj->isa('Yote::YapiServer::User')) { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # User objects - owner is self | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return (0, "owner access required") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | unless $target_obj->id eq $user->id; | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # Admin-only | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | if ($method_def->{admin_only}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return (0, "admin access required") unless $user->get_is_admin; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | return (1); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # Connection metadata (what client receives on connect) | ||||||||||||||||||||||||||||||||||||||||||
| 121 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | sub connect_info_methods { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 6 | ▶ | my ($self, $session, $user) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connect_info_methods: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 6 | my $methods = $self->method_defs; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 6 | my @available_methods; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 6 | for my $method (sort keys %$methods) { | |||||||||||||||||||||||||||||||||||||||||
| 130 | 22 | my $def = $methods->{$method}; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # Include public methods always | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 22 | ▶ | if ($def->{public}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 6 | push @available_methods, $method; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # Include auth methods if user is logged in | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 16 | ▶ | if ($user && ($def->{auth} || $def->{owner_only})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$user and $def->{"auth"} || $def->{"owner_only"}
$def->{"auth"} or $def->{"owner_only"}
MC/DC66%2/3
$user and $def->{"auth"} || $def->{"owner_only"}
$user
$def->{"auth"}
$def->{"owner_only"}
Truth table75%3/4
A and B or C
A= $userB= $def->{"auth"}C= $def->{"owner_only"}
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 6 | push @available_methods, $method; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | # Include admin methods if user is admin | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 10 | ▶ | if ($user && $user->get_is_admin && $def->{admin_only}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$user and $user->get_is_admin and $def->{"admin_only"}
$user and $user->get_is_admin
MC/DC66%2/3
$user and $user->get_is_admin and $def->{"admin_only"}
$user
$user->get_is_admin
$def->{"admin_only"}
Truth table75%3/4
A and B and C
A= $userB= $user->get_is_adminC= $def->{"admin_only"}
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | push @available_methods, $method; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 6 | return \@available_methods; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||||||||||||||||