lib/CallBackery/GuiPlugin/UserForm.pm

filter: Statementstmt 37.3% 34 / 91 Branchbran 10.4% 5 / 48 Conditioncond 13.8% 5 / 36 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 66.6% 8 / 12 Podpod 100% 1 / 1 totaltotal 25.9% 54 / 208 CC 41 SCAR 50.8
CC
41
Cov
25%
__ANON__
380.0
__ANON__
72.0
getAllFieldValues
56.0
CRAP 160.6
↑ Summary blib/lib/CallBackery/Controller/RpcService.pm »
1 package CallBackery::GuiPlugin::UserForm;
2 1 ▶use Mojo::Base 'CallBackery::GuiPlugin::AbstractForm';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use CallBackery::Translate qw(trm);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use CallBackery::Exception qw(mkerror);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Util qw(hmac_sha1_sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::JSON qw(true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 CallBackery::GuiPlugin::UserForm - UserForm Plugin
11  
12 =head1 SYNOPSIS
13  
14 use CallBackery::GuiPlugin::UserForm;
15  
16 =head1 DESCRIPTION
17  
18 The UserForm Plugin.
19  
20 =cut
21  
22  
23 =head1 PROPERTIES
24  
25 All the methods of L<CallBackery::GuiPlugin::AbstractForm> plus:
26  
27 =cut
28  
29 =head2 formCfg
30  
31 Returns a Configuration Structure for the Report Frontend Module.
32  
33 =cut
34  
35 1 my $DUMMY_PASSWORD = '>>NOt REALly a PaSSwoRd<<';
36  
37 0 ▶has formCfg => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
38 0 my $self = shift;
39  
40 0 ▶ if ($self->config->{type} eq 'edit' and not $self->args->{selection}{cbuser_id}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"type"} eq "edit" and not $self->args->{"selection"}{"cbuser_id"}) 0 0 0
Condition0%0/3
$self->config->{"type"} eq "edit" and not $self->args->{"selection"}{"cbuser_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->config->{"type"} eq "edit" and not $self->args->{"selection"}{"cbuser_id"}
$self->config->{"type"} eq "edit" not $self->args->{"selection"}{"cbuser_id"}
Truth table0%0/3
A and not(B)
A= $self->config->{"type"} eq "edit"B= not $self->args->{"selection"}{"cbuser_id"}
ABresult
0X0
100
111
41 return [{
42 0 label => trm('Error'),
43 widget => 'header',
44 note => trm('No user selected.')
45 }];
46 }
47 return [
48 $self->config->{type} eq 'edit' ? {
49 key => 'cbuser_id',
50 label => trm('UserId'),
51 widget => 'hiddenText',
52 set => {
53 readOnly => true,
54 },
55 } : (),
56  
57 {
58 key => 'cbuser_login',
59 label => trm('Login'),
60 widget => 'text',
61 set => {
62 required => true,
63 readOnly => $self->user->may('admin') ? false : true
64 },
65 },
66 {
67 key => 'cbuser_password',
68 label => trm('Password'),
69 widget => 'password',
70 set => {
71 required => true,
72 },
73 },
74 {
75 key => 'cbuser_password_check',
76 label => trm('Password Again'),
77 widget => 'password',
78 set => {
79 required => true,
80 },
81 },
82  
83 {
84 key => 'cbuser_given',
85 label => trm('Given Name'),
86 widget => 'text',
87 set => {
88 required => true,
89 readOnly => $self->user->may('admin') ? false : true
90 },
91 },
92 {
93 key => 'cbuser_family',
94 label => trm('Family Name'),
95 widget => 'text',
96 set => {
97 required => true,
98 readOnly => $self->user->may('admin') ? false : true
99 }
100 },
101 {
102 key => 'cbuser_note',
103 label => trm('Note'),
104 widget => 'textArea',
105 set => {
106 placeholder => 'some extra information about this user',
107 readOnly => $self->user->may('admin') ? false : true
108 }
109 },
110 0 ▶ @{$self->rightsCheckBoxes}
Branch: 5 branches
BranchTrueFalseTotal
$self->config->{'type'} eq 'edit' ? : 0 0 0
$self->user->may('admin') ? : 0 0 0
$self->user->may('admin') ? : 0 0 0
$self->user->may('admin') ? : 0 0 0
$self->user->may('admin') ? : 0 0 0
111 ];
112 1 };
113  
114 0 ▶has rightsCheckBoxes => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
115 0 my $self = shift;
116 0 ▶ return [] if not $self->user->may('admin');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user->may("admin") 0 0 0
117 0 my $db = $self->user->mojoSqlDb;
118 0 my $rightTbl= $db->dbh->quote_identifier('cbright');
119 0 my $rights = $db->dbh->selectall_arrayref(<<"SQL",{Slice => {}});
120 SELECT cbright_id,cbright_label FROM $rightTbl
121 SQL
122 0 my @checkboxes = {
123 widget => 'header',
124 label => 'Permissions'
125 };
126 0 my $adminId = $self->user->db->lookUp('cbright','key','admin');
127  
128 0 for (sort {$a->{cbright_label} cmp $b->{cbright_label}} @$rights){
129 push @checkboxes,
130 {
131 key => 'cbright_id_'.$_->{cbright_id},
132 widget => 'checkBox',
133 label => $_->{cbright_label},
134 }
135 0 }
136 0 return \@checkboxes;
137 1 };
138  
139  
140 3 ▶has actionCfg => sub {
Subroutine __ANON__: called CC=5 SCAR=30.1
141 3 my $self = shift;
142 3 ▶ my $mode = $self->config->{mode} // 'default';
Condition100%2/2
$self->config->{"mode"} // "default"
l!l
12
MC/DC100%1/1
$self->config->{"mode"} // "default"
$self->config->{"mode"}
Truth table100%2/2
A
A= $self->config->{"mode"}
Aresult
11
00
143 3 ▶ my $type = $self->config->{type} // 'new';
Condition50%1/2
$self->config->{"type"} // "new"
l!l
30
MC/DC0%0/1
$self->config->{"type"} // "new"
$self->config->{"type"}
Truth table50%1/2
A
A= $self->config->{"type"}
Aresult
11
00
144  
145 0 ▶ my $handler = sub {
Subroutine __ANON__: not called CC=19 SCAR=59.4
146 0 my $self = shift;
147 0 my $args = shift;
148 0 ▶ my $admin = ($self->user->may('admin') or $mode eq 'init');
Condition0%0/3
$self->user->may('admin') || $mode eq 'init'
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->user->may('admin') || $mode eq 'init'
$self->user->may('admin') $mode eq 'init'
Truth table0%0/3
A || B
A= $self->user->may('admin')B= $mode eq 'init'
ABresult
1X1
011
000
149  
150 0 ▶ my @fields = $admin ? (qw(login family given note)) : ();
Branch: 1 branch
BranchTrueFalseTotal
$admin ? : 0 0 0
151 0 my $itsMine = $args->{cbuser_id} == $self->user->userId;
152 0 ▶ die mkerror(2847,"You can only edit your own stuff unless you have admin permissions.")
Branch: 1 branch
BranchTrueFalseTotal
unless $admin or $itsMine 0 0 0
Condition0%0/3
$admin or $itsMine
l!l&&r!l&&!r
000
MC/DC0%0/2
$admin or $itsMine
$admin $itsMine
Truth table0%0/3
A or B
A= $adminB= $itsMine
ABresult
1X1
011
000
153 unless $admin or $itsMine;
154  
155 0 ▶ if ($args->{cbuser_password} ne $DUMMY_PASSWORD){
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"cbuser_password"} ne $DUMMY_PASSWORD) 0 0 0
156 die mkerror(2847,"The password instances did not match.")
157 0 ▶ if $args->{cbuser_password} ne $args->{cbuser_password_check};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"cbuser_password"} ne $args->{"cbuser_password_check"} 0 0 0
158 0 push @fields, 'password';
159 }
160  
161 0 $args->{cbuser_password} = hmac_sha1_sum($args->{cbuser_password});
162 0 my $db = $self->user->db;
163  
164 $args->{cbuser_id} = $db->updateOrInsertData('cbuser',{
165 0 ▶ map { $_ => $args->{'cbuser_'.$_} } @fields
Branch: 1 branch
BranchTrueFalseTotal
$args->{'cbuser_id'} ? : 0 0 0
166 0 },$args->{cbuser_id} ? { id => int($args->{cbuser_id}) } : ());
167  
168  
169 0 my $adminId = $db->fetchValue('cbright',{key=>'admin'},'id');
170 0 ▶ if ($admin){
Branch: 1 branch
BranchTrueFalseTotal
if ($admin) 0 0 0
171 0 for (keys %$args){
172 0 ▶ next if not /^cbright_id_(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^cbright_id_(\d+)$/u 0 0 0
173 0 my $right_id = $1;
174 my $match = {
175 cbuser => $args->{cbuser_id},
176 0 cbright => $right_id
177 };
178 # in init mode the user gets admin ... always
179 0 ▶ if ($args->{$_}
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{$_} or $mode eq 'init' and $right_id == $adminId or $itsMine and $right_id == $adminId) { } 0 0 0
Condition0%0/12
$args->{$_} or $mode eq "init" and $right_id == $adminId or $itsMine and $right_id == $adminId
l!l&&r!l&&!r
000
$args->{$_} or $mode eq "init" and $right_id == $adminId
l!l&&r!l&&!r
000
$mode eq "init" and $right_id == $adminId
!ll&&!rl&&r
000
$itsMine and $right_id == $adminId
!ll&&!rl&&r
000
MC/DC0%0/5
$args->{$_} or $mode eq "init" and $right_id == $adminId or $itsMine and $right_id == $adminId
$args->{$_} $mode eq "init" $right_id == $adminId $itsMine $right_id == $adminId
Truth table0%0/8
A or B and C or D and E
A= $args->{$_}B= $mode eq "init"C= $right_id == $adminIdD= $itsMineE= $right_id == $adminId
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
180 or ( $mode eq 'init' and $right_id == $adminId )
181 or ( $itsMine and $right_id == $adminId )
182 ){
183 0 $db->updateOrInsertData('cbuserright',$match,$match);
184 }
185 else {
186 0 $db->deleteDataWhere('cbuserright',$match);
187 }
188 }
189 }
190  
191 0 ▶ if ($self->controller and $self->controller->can('runEventActions')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->controller and $self->controller->can("runEventActions")) 0 0 0
Condition0%0/3
$self->controller and $self->controller->can("runEventActions")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->controller and $self->controller->can("runEventActions")
$self->controller $self->controller->can("runEventActions")
Truth table0%0/3
A and B
A= $self->controllerB= $self->controller->can("runEventActions")
ABresult
0X0
100
111
192 0 $self->controller->runEventActions('changeConfig');
193 }
194 return {
195 0 ▶ action => $mode eq 'init' ? 'logout' : 'dataSaved'
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'init' ? : 0 0 0
196 };
197 3 };
198  
199 return [
200 {
201 3 ▶ label => $mode eq 'init'
Branch: 2 branches
BranchTrueFalseTotal
$mode eq 'init' ? : 1 2 3
$type eq 'edit' ? : 1 1 2
202 ? trm('Create Admin Account')
203 : $type eq 'edit'
204 ? trm('Save Changes')
205 : trm('Add User'),
206 action => 'submit',
207 key => 'save',
208 actionHandler => $handler
209 }
210 ];
211 1 };
212  
213 1 ▶has grammar => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 1 my $self = shift;
215 1 $self->mergeGrammar(
216 $self->SUPER::grammar,
217 {
218 _doc => "User Form Configuration",
219 _vars => [ qw(type mode) ],
220 type => {
221 _doc => 'type of form to show: edit, add',
222 _re => '(edit|add)'
223 },
224 mode => {
225 _doc => 'In init mode the for will run for the __ROOT user and thus allow the creation of the initial account',
226 _re => '(init|default)',
227 _re_error => 'Pick one of init or default',
228 _default => 'default'
229 }
230 },
231 );
232 1 };
233  
234 1 ▶has checkAccess => sub {
Subroutine __ANON__: called CC=4 SCAR=15.9
235 1 my $self = shift;
236 1 my $userId = $self->user->userId;
237 1 ▶ my $mode = $self->config->{mode} // 'default';
Condition50%1/2
$self->config->{"mode"} // "default"
l!l
10
MC/DC0%0/1
$self->config->{"mode"} // "default"
$self->config->{"mode"}
Truth table50%1/2
A
A= $self->config->{"mode"}
Aresult
11
00
238 1 ▶ if ($mode eq 'init'){
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "init") 1 0 1
239 1 ▶ return ($userId and $userId eq '__ROOT');
Condition33%1/3
$userId && $userId eq '__ROOT'
!ll&&!rl&&r
010
MC/DC0%0/2
$userId && $userId eq '__ROOT'
$userId $userId eq '__ROOT'
Truth table33%1/3
A && B
A= $userIdB= $userId eq '__ROOT'
ABresult
0X0
100
111
240 }
241 0 return $self->user;
242 1 };
243  
244 =head1 METHODS
245  
246 All the methods of L<CallBackery::GuiPlugin::AbstractForm> plus:
247  
248 =cut
249  
250  
251 0 ▶sub getAllFieldValues {
Subroutine getAllFieldValues: not called CC=7 SCAR=40.3
Pod documented
252 0 my $self = shift;
253 0 my $args = shift;
254 0 ▶ return {} if $self->config->{type} ne 'edit';
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"type"} ne "edit" 0 0 0
255 0 my $id = $args->{selection}{cbuser_id};
256  
257 0 ▶ return {} unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
258  
259 0 ▶ die mkerror(2847,"You can only edit your own stuff unless you have admin permissions.")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user->may("admin") or $id == $self->user->userId 0 0 0
Condition0%0/3
$self->user->may("admin") or $id == $self->user->userId
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->user->may("admin") or $id == $self->user->userId
$self->user->may("admin") $id == $self->user->userId
Truth table0%0/3
A or B
A= $self->user->may("admin")B= $id == $self->user->userId
ABresult
1X1
011
000
260 if not ( $self->user->may('admin') or $id == $self->user->userId );
261  
262 0 my $data = $self->user->db->fetchRow('cbuser',{id => $id});
263 0 $data->{cbuser_password} = $DUMMY_PASSWORD;
264 0 $data->{cbuser_password_check} = $DUMMY_PASSWORD;
265 0 my $db = $self->user->mojoSqlDb;
266 0 my $rightTbl= $db->dbh->quote_identifier('cbright');
267 0 my $rights = $db->dbh->selectall_arrayref(<<"SQL",{Slice => {}}, $id);
268 SELECT cbright_id, cbuserright_id IS NOT NULL as value
269 FROM $rightTbl LEFT JOIN cbuserright ON (cbright_id = cbuserright_cbright AND cbuserright_cbuser = ?)
270 SQL
271 0 for (@$rights){
272 0 ▶ $data->{'cbright_id_'.$_->{cbright_id}} = $_->{value} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'value'} ? : 0 0 0
273 }
274  
275 0 return $data;
276 }
277  
278 1 1;
279 __END__
↑ Summary blib/lib/CallBackery/Controller/RpcService.pm »