| 1 | package CallBackery::GuiPlugin::Users; | ||||||||||||||||||||
| 2 | 11 | ▶ | use Mojo::Base 'CallBackery::GuiPlugin::AbstractTable'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 11 | ▶ | use CallBackery::Translate qw(trm); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 11 | ▶ | use CallBackery::Exception qw(mkerror); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 11 | ▶ | use Mojo::JSON qw(true false); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | CallBackery::GuiPlugin::Users - User Plugin | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | use CallBackery::GuiPlugin::Users; | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | The User Plugin. | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 PROPERTIES | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | All the methods of L<CallBackery::GuiPlugin::AbstractTable> plus: | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head2 tableCfg | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 0 | ▶ | has tableCfg => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 34 | 0 | my $self = shift; | |||||||||||||||||||
| 35 | 0 | ▶ | my $admin = ( not $self->user or $self->user->may('admin')); | ||||||||||||||||||
|
Condition0%0/3
!$self->user || $self->user->may('admin')
MC/DC0%0/2
!$self->user || $self->user->may('admin')
!$self->user
$self->user->may('admin')
Truth table0%0/3
not(A) || B
A= !$self->userB= $self->user->may('admin')
| |||||||||||||||||||||
| 36 | return [ | ||||||||||||||||||||
| 37 | { | ||||||||||||||||||||
| 38 | 0 | ▶ | label => trm('UserId'), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | type => 'number', | ||||||||||||||||||||
| 40 | width => '1*', | ||||||||||||||||||||
| 41 | key => 'cbuser_id', | ||||||||||||||||||||
| 42 | sortable => true, | ||||||||||||||||||||
| 43 | primary => true, | ||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||
| 45 | { | ||||||||||||||||||||
| 46 | label => trm('Username'), | ||||||||||||||||||||
| 47 | type => 'string', | ||||||||||||||||||||
| 48 | width => '3*', | ||||||||||||||||||||
| 49 | key => 'cbuser_login', | ||||||||||||||||||||
| 50 | sortable => true, | ||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||
| 52 | { | ||||||||||||||||||||
| 53 | label => trm('Given Name'), | ||||||||||||||||||||
| 54 | type => 'string', | ||||||||||||||||||||
| 55 | width => '4*', | ||||||||||||||||||||
| 56 | key => 'cbuser_given', | ||||||||||||||||||||
| 57 | sortable => true, | ||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||
| 59 | { | ||||||||||||||||||||
| 60 | label => trm('Family Name'), | ||||||||||||||||||||
| 61 | type => 'string', | ||||||||||||||||||||
| 62 | width => '4*', | ||||||||||||||||||||
| 63 | key => 'cbuser_family', | ||||||||||||||||||||
| 64 | sortable => true, | ||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||
| 66 | { | ||||||||||||||||||||
| 67 | label => trm('Rights'), | ||||||||||||||||||||
| 68 | type => 'string', | ||||||||||||||||||||
| 69 | sortable => false, | ||||||||||||||||||||
| 70 | width => '8*', | ||||||||||||||||||||
| 71 | key => 'cbuser_cbrights', | ||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||
| 73 | $admin ? ({ | ||||||||||||||||||||
| 74 | label => trm('Note'), | ||||||||||||||||||||
| 75 | type => 'string', | ||||||||||||||||||||
| 76 | width => '8*', | ||||||||||||||||||||
| 77 | key => 'cbuser_note', | ||||||||||||||||||||
| 78 | }):(), | ||||||||||||||||||||
| 79 | ] | ||||||||||||||||||||
| 80 | 11 | }; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head2 actionCfg | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 12 | ▶ | has actionCfg => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=25.2
| |||||||||||||||||||||
| 87 | 12 | my $self = shift; | |||||||||||||||||||
| 88 | # we must be in admin mode if no user property is set to have be able to prototype all forms variants | ||||||||||||||||||||
| 89 | 12 | ▶ | my $admin = ( not $self->user or $self->user->may('admin')); | ||||||||||||||||||
|
Condition33%1/3
!$self->user || $self->user->may('admin')
MC/DC0%0/2
!$self->user || $self->user->may('admin')
!$self->user
$self->user->may('admin')
Truth table33%1/3
not(A) || B
A= !$self->userB= $self->user->may('admin')
| |||||||||||||||||||||
| 90 | return [ | ||||||||||||||||||||
| 91 | $admin ? ({ | ||||||||||||||||||||
| 92 | label => trm('Add User'), | ||||||||||||||||||||
| 93 | action => 'popup', | ||||||||||||||||||||
| 94 | addToContextMenu => true, | ||||||||||||||||||||
| 95 | key => 'add', | ||||||||||||||||||||
| 96 | popupTitle => trm('New User'), | ||||||||||||||||||||
| 97 | backend => { | ||||||||||||||||||||
| 98 | plugin => 'UserForm', | ||||||||||||||||||||
| 99 | config => { | ||||||||||||||||||||
| 100 | type => 'add' | ||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | }) : (), | ||||||||||||||||||||
| 104 | { | ||||||||||||||||||||
| 105 | label => trm('Edit User'), | ||||||||||||||||||||
| 106 | action => 'popup', | ||||||||||||||||||||
| 107 | addToContextMenu => true, | ||||||||||||||||||||
| 108 | defaultAction => true, | ||||||||||||||||||||
| 109 | key => 'edit', | ||||||||||||||||||||
| 110 | popupTitle => trm('Edit User'), | ||||||||||||||||||||
| 111 | 0 | ▶ | actionHandler => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||
| 113 | 0 | my $args = shift; | |||||||||||||||||||
| 114 | 0 | my $id = $args->{selection}{cbuser_id}; | |||||||||||||||||||
| 115 | 0 | ▶ | die mkerror(393,trm('You have to select a user first')) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | if not $id; | ||||||||||||||||||||
| 117 | }, | ||||||||||||||||||||
| 118 | set => { | ||||||||||||||||||||
| 119 | height => 340, | ||||||||||||||||||||
| 120 | width => 500 | ||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||
| 122 | backend => { | ||||||||||||||||||||
| 123 | plugin => 'UserForm', | ||||||||||||||||||||
| 124 | config => { | ||||||||||||||||||||
| 125 | type => 'edit' | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||
| 129 | $admin ? ({ | ||||||||||||||||||||
| 130 | label => trm('Delete User'), | ||||||||||||||||||||
| 131 | action => 'submitVerify', | ||||||||||||||||||||
| 132 | addToContextMenu => true, | ||||||||||||||||||||
| 133 | question => trm('Do you really want to delete the selected user ?'), | ||||||||||||||||||||
| 134 | key => 'delete', | ||||||||||||||||||||
| 135 | 0 | ▶ | actionHandler => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 136 | 0 | my $self = shift; | |||||||||||||||||||
| 137 | 0 | my $args = shift; | |||||||||||||||||||
| 138 | 0 | my $id = $args->{selection}{cbuser_id}; | |||||||||||||||||||
| 139 | 0 | ▶ | die mkerror(4992,trm("You have to select a user first")) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | if not $id; | ||||||||||||||||||||
| 141 | 0 | ▶ | die mkerror(4993,trm("You can not delete the user you are logged in with")) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | if $id == $self->user->userId; | ||||||||||||||||||||
| 143 | 0 | my $db = $self->user->db; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | ▶ | if ($db->deleteData('cbuser',$id) == 1){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | return { | ||||||||||||||||||||
| 147 | 0 | action => 'reload', | |||||||||||||||||||
| 148 | }; | ||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | 0 | die mkerror(4993,trm("Faild to remove user %1",$id)); | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | 12 | ▶ | }) : (), | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 153 | ]; | ||||||||||||||||||||
| 154 | 11 | }; | |||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =head1 METHODS | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | All the methods of L<CallBackery::GuiPlugin::AbstractForm> plus: | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 0 | ▶ | sub currentUserFilter { | ||||||||||||||||||
|
Subroutine
currentUserFilter: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 164 | 0 | my $self = shift; | |||||||||||||||||||
| 165 | 0 | ▶ | if (not $self->user->may('admin')){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 0 | return 'WHERE cbuser_id = ' . $self->user->mojoSqlDb->dbh->quote($self->user->userId); | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | 0 | return ''; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | ▶ | sub getTableRowCount { | ||||||||||||||||||
|
Subroutine
getTableRowCount: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||
| 173 | 0 | my $args = shift; | |||||||||||||||||||
| 174 | 0 | my $db = $self->user->mojoSqlDb; | |||||||||||||||||||
| 175 | 0 | ▶ | if ($self->user->may('admin')){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | 0 | return [$db->dbh->selectrow_array('SELECT count(cbuser_id) FROM ' | |||||||||||||||||||
| 177 | . $db->dbh->quote_identifier('cbuser'))]->[0]; | ||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | 0 | return 1; | |||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 1 | ▶ | sub getTableData { | ||||||||||||||||||
|
Subroutine
getTableData: called CC=8 SCAR=22.6
Pod
documented
| |||||||||||||||||||||
| 183 | 1 | my $self = shift; | |||||||||||||||||||
| 184 | 1 | my $args = shift; | |||||||||||||||||||
| 185 | 1 | my $db = $self->user->mojoSqlDb; | |||||||||||||||||||
| 186 | 1 | my $SORT =''; | |||||||||||||||||||
| 187 | 1 | ▶ | if ($args->{sortColumn}){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 188 | 0 | $SORT = 'ORDER BY '.$db->dbh->quote_identifier($args->{sortColumn}); | |||||||||||||||||||
| 189 | 0 | ▶ | $SORT .= $args->{sortDesc} ? ' DESC' : ' ASC'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | 1 | my $WHERE = ''; | |||||||||||||||||||
| 192 | 1 | ▶ | if (not $self->user->may('admin')){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | 1 | $WHERE = 'WHERE cbuser_id = ' . $db->dbh->quote($self->user->userId); | |||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | 1 | my $userTbl = $db->dbh->quote_identifier('cbuser'); | |||||||||||||||||||
| 196 | 1 | my $rightTbl = $db->dbh->quote_identifier('cbright'); | |||||||||||||||||||
| 197 | 1 | my $data = $db->dbh->selectall_arrayref(<<"SQL",{Slice => {}}, $args->{lastRow}-$args->{firstRow}+1,$args->{firstRow}); | |||||||||||||||||||
| 198 | SELECT cbuser_id,cbuser_login, cbuser_given, cbuser_family, cbuser_note | ||||||||||||||||||||
| 199 | FROM $userTbl | ||||||||||||||||||||
| 200 | $WHERE | ||||||||||||||||||||
| 201 | $SORT | ||||||||||||||||||||
| 202 | LIMIT ? OFFSET ? | ||||||||||||||||||||
| 203 | SQL | ||||||||||||||||||||
| 204 | 1 | my @keys = map { $_->{cbuser_id} } @$data; | |||||||||||||||||||
| 205 | 1 | my $keyPh = join ',', map { '?' } @keys; | |||||||||||||||||||
| 206 | 1 | my $rightList = $db->dbh->selectall_arrayref(<<"SQL",{Slice => {}}, @keys ); | |||||||||||||||||||
| 207 | SELECT cbuserright_cbuser,cbright_label FROM cbuserright JOIN $rightTbl ON cbuserright_cbright = cbright_id WHERE cbuserright_cbuser IN ($keyPh) | ||||||||||||||||||||
| 208 | SQL | ||||||||||||||||||||
| 209 | 1 | my %rights; | |||||||||||||||||||
| 210 | 1 | for (@$rightList){ | |||||||||||||||||||
| 211 | 0 | push @{$rights{$_->{cbuserright_cbuser}}}, $_->{cbright_label}; | |||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | 1 | my $admin = $self->user->may('admin'); | |||||||||||||||||||
| 214 | 1 | for (@$data){ | |||||||||||||||||||
| 215 | 1 | ▶ | delete $_->{cbuser_note} unless $admin; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | 0 | ▶ | $_->{cbuser_cbrights} = join ', ', sort @{$rights{$_->{cbuser_id}}} if ref $rights{$_->{cbuser_id}} eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | } | ||||||||||||||||||||
| 218 | 1 | return $data; | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 11 | 1; | |||||||||||||||||||
| 223 | __END__ | ||||||||||||||||||||