lib/Yote/YapiServer/App/Base.pm

filter: Statementstmt 96.9% 64 / 66 Branchbran 71.0% 27 / 38 Conditioncond 72.4% 21 / 29 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 5 totaltotal 78.1% 129 / 165 CC 31 SCAR 25.0
CC
31
Cov
84%
authorize_object_method
24.1
connect_info_methods
9.0
authorize_method
7.0
CRAP 12.1
« blib/lib/Yote/YapiServer/User.pm ↑ Summary lib/Yote/YapiServer/YapiDef.pm »
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
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
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]
l!l&&r!l&&!r
120
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
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
l!l
30
MC/DC0%0/1
${"${class}::app_version";} // 1
${"${class}::app_version";}
Truth table50%1/2
A
A= ${"${class}::app_version";}
Aresult
11
00
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
BranchTrueFalseTotal
unless $method_def 1 11 12
64  
65 # Public methods - no auth required
66 11 ▶ return (1) if $method_def->{public};
Branch: 1 branch
BranchTrueFalseTotal
if $method_def->{"public"} 5 6 11
67  
68 # All other methods require authentication
69 6 ▶ return (0, "authentication required") unless $session && $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $session and $user 2 4 6
Condition100%3/3
$session and $user
!ll&&!rl&&r
114
MC/DC100%2/2
$session and $user
$session $user
Truth table100%3/3
A and B
A= $sessionB= $user
ABresult
0X0
100
111
70  
71 # Admin-only methods
72 4 ▶ if ($method_def->{admin_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_def->{"admin_only"}) 2 2 4
73 2 ▶ return (0, "admin access required") unless $user->get_is_admin;
Branch: 1 branch
BranchTrueFalseTotal
unless $user->get_is_admin 1 1 2
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
BranchTrueFalseTotal
unless $session->can_access($target_obj) 1 4 5
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
BranchTrueFalseTotal
unless $method_def 0 4 4
94 4 ▶ return (1) if $method_def->{public};
Branch: 1 branch
BranchTrueFalseTotal
if $method_def->{"public"} 2 2 4
95 2 ▶ return (0, "authentication required") unless $session && $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $session and $user 0 2 2
Condition33%1/3
$session and $user
!ll&&!rl&&r
002
MC/DC0%0/2
$session and $user
$session $user
Truth table33%1/3
A and B
A= $sessionB= $user
ABresult
0X0
100
111
96  
97 # Owner-only: object must belong to calling user
98 2 ▶ if ($method_def->{owner_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_def->{"owner_only"}) 2 0 2
99 2 ▶ if ($target_obj->can('get_owner')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($target_obj->can('get_owner')) { } 2 0 2
elsif ($target_obj->isa('Yote::YapiServer::User')) { } 0 0 0
100 2 my $owner = $target_obj->get_owner;
101 2 ▶ return (0, "owner access required")
Branch: 1 branch
BranchTrueFalseTotal
unless $owner and $owner->id eq $user->id 1 1 2
Condition66%2/3
$owner and $owner->id eq $user->id
!ll&&!rl&&r
011
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $target_obj->id eq $user->id 0 0 0
106 unless $target_obj->id eq $user->id;
107 }
108 }
109  
110 # Admin-only
111 1 ▶ if ($method_def->{admin_only}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_def->{"admin_only"}) 0 1 1
112 0 ▶ return (0, "admin access required") unless $user->get_is_admin;
Branch: 1 branch
BranchTrueFalseTotal
unless $user->get_is_admin 0 0 0
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
BranchTrueFalseTotal
if ($def->{"public"}) 6 16 22
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
BranchTrueFalseTotal
if ($user and $def->{"auth"} || $def->{"owner_only"}) 6 10 16
Condition100%6/6
$user and $def->{"auth"} || $def->{"owner_only"}
!ll&&!rl&&r
736
$def->{"auth"} or $def->{"owner_only"}
l!l&&r!l&&!r
333
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"}
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
if ($user and $user->get_is_admin and $def->{"admin_only"}) 1 9 10
Condition83%5/6
$user and $user->get_is_admin and $def->{"admin_only"}
!ll&&!rl&&r
901
$user and $user->get_is_admin
!ll&&!rl&&r
721
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"}
ABCresult
0XX0
10X0
1100
1111
146 1 push @available_methods, $method;
147 }
148 }
149  
150 6 return \@available_methods;
151 }
152  
153 2 1;
154  
155 __END__
« blib/lib/Yote/YapiServer/User.pm ↑ Summary lib/Yote/YapiServer/YapiDef.pm »