XTaTIK/Controller/User.pm

filter: Statementstmt 10.0% 11 / 109 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 15.0% 3 / 20 Podpod 0.0% 0 / 15 totaltotal 7.6% 14 / 184 CC 29 SCAR 42.0
CC
29
Cov
7%
site_products
210.0
master_products_database_post
20.0
master_products_database_update
12.0
CRAP 67.0
« XTaTIK/Plugin/Cart/PayPal.pm ↑ Summary XTaTIK/Model/Blog.pm »
1 package XTaTIK::Controller::User;
2  
3 1 our $VERSION = '0.005003'; # VERSION
4  
5 1 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use experimental 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::Meth qw/$json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub login {
Subroutine login: not called CC=2 SCAR=17.9
Pod undocumented
10 0 my $self = shift;
11  
12 0 my $user = $self->users->check(
13 $self->param('login'), $self->param('pass')
14 );
15  
16 0 ▶ if ( $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) { } 0 0 0
17 0 $self->session(
18 is_logged_in => 1,
19 user => $user,
20 );
21 0 $self->redirect_to('user/index');
22 }
23 else {
24 0 $self->flash( is_login_failed => 1 );
25 0 $self->redirect_to('/login');
26 }
27 }
28  
29 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod undocumented
30 0 my $self = shift;
31 0 $self->session( is_logged_in => 0 );
32 0 $self->redirect_to('/login');
33 }
34  
35 0 ▶sub is_logged_in {
Subroutine is_logged_in: not called CC=2 SCAR=17.9
Pod undocumented
36 0 my $self = shift;
37  
38 0 ▶ return 1 if $self->session('is_logged_in');
Branch: 1 branch
BranchTrueFalseTotal
if $self->session("is_logged_in") 0 0 0
39 0 return $self->redirect_to('/login');
40 }
41  
42 0 ▶sub site_products {
Subroutine site_products: not called CC=14 SCAR=53.5
Pod undocumented
43 0 my $self = shift;
44  
45 0 ▶ if ( $self->param('save') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("save")) 0 0 0
46 0 my %to_remove = map +( $_->{number} => $_->{id} ),
47 $self->products->get_all( $self->config('site') )->@*;
48  
49 0 my @lines;
50 0 for ( split /\n/, $self->param('products') ) {
51 0 ▶ /^\s*#?(\S+)(?:\s+(\S+))?/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*#?(\S+)(?:\s+(\S+))?/u or next 0 0 0
52  
53 0 my $line = { num => $1, price => $2 };
54 0 ▶ $line->{price} //= '00_0.00';
Condition0%0/2
$line->{"price"} //= "00_0.00"
l!l
00
MC/DC0%0/1
$line->{"price"} //= "00_0.00"
$line->{"price"}
Truth table0%0/2
A
A= $line->{"price"}
Aresult
11
00
55 $line->{price} = join ',', map tr/$//dr,
56 0 ▶ map +(/_/ ? $_ : "00_$_"), split /,/, $line->{price};
Branch: 1 branch
BranchTrueFalseTotal
/_/u ? : 0 0 0
57  
58 0 push @lines, $line;
59 0 delete $to_remove{ $line->{num} };
60 }
61  
62 # Dump removed products from the site and its search index
63 $self->products->unset_site(
64 0 ▶ $self->config('site'), [keys %to_remove]
Branch: 1 branch
BranchTrueFalseTotal
if keys %to_remove 0 0 0
65 ) if keys %to_remove;
66  
67 0 $self->product_search->delete( $_ ) for values %to_remove;
68  
69 $self->products
70 0 ->set_site( $self->config('site'), [ map $_->{num}, @lines ], );
71  
72 0 $self->products->set_pricing( \@lines );
73 }
74  
75 0 my $products = $self->products->get_all( $self->config('site') );
76  
77 # update search index
78 0 ▶ if ( $self->param('save') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param("save")) 0 0 0
79 0 for ( @$products ) {
80 $self->product_search->delete( $_->{id} )->add(
81 $_->{id},
82 join ' ', grep defined,
83 0 @$_{qw/number group_desc title category description tip_description quote_description/}
84 );
85 }
86 }
87  
88 0 my @prods = $self->products->get_all( $self->config('site') )->@*;
89 0 my @list;
90 0 for ( @prods ) {
91 0 my $pr = $_->{price_raw}->$json->{ $self->config('site') };
92 0 ▶ if ( ref $pr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pr) 0 0 0
93 0 $pr = join ',', map "${_}_$pr->{$_}", sort keys %$pr;
94 }
95 0 ▶ $pr =~ s/\b00_// if $pr;
Branch: 1 branch
BranchTrueFalseTotal
if $pr 0 0 0
96 0 ▶ push @list, "$_->{number}\t" . ($pr // '0.00');
Condition0%0/2
$pr // "0.00"
l!l
00
MC/DC0%0/1
$pr // "0.00"
$pr
Truth table0%0/2
A
A= $pr
Aresult
11
00
97 }
98 0 $self->param( products => join "\n", @list );
99 }
100  
101 0 ▶sub master_products_database {
Subroutine master_products_database: not called CC=1 SCAR=6.9
Pod undocumented
102 0 my $self = shift;
103  
104 0 $self->stash( products => $self->products->get_all('*') );
105 }
106  
107 0 ▶sub master_products_database_post {
Subroutine master_products_database_post: not called CC=4 SCAR=30.0
Pod undocumented
108 0 my $self = shift;
109  
110 0 $self->form_checker(
111 rules => {
112 number => { max => 1000, },
113 ( map +( $_ => { optional => 1, max => 1000 } ),
114 qw/image title category group_master
115 group_desc unit/ ),
116 ( map +( $_ => { optional => 1, max => 1000_000 } ),
117 qw/description tip_description quote_description
118 recommended/ ),
119 },
120 );
121  
122 0 ▶ return $self->render( template => 'user/master_products_database' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->form_checker_ok 0 0 0
123 unless $self->form_checker_ok;
124  
125 # Check CSRF token
126 0 ▶ return $self->render(text => 'Bad CSRF token!', status => 403)
Branch: 1 branch
BranchTrueFalseTotal
if $self->validation->csrf_protect->has_error("csrf_token") 0 0 0
127 if $self->validation->csrf_protect->has_error('csrf_token');
128  
129 0 ▶ if ( $self->products->exists( $self->param('number') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->products->exists($self->param("number"))) 0 0 0
130 0 $self->stash( already_have_this_product => 1 );
131 0 return $self->render( template => 'user/master_products_database' );
132 }
133  
134 0 $self->stash( product_add_ok => 1 );
135 0 $self->products->add(
136 map +( $_ => $self->param( $_ ) ),
137 qw/number image title category group_master
138 group_desc unit description tip_description quote_description recommended/,
139 );
140  
141 0 $self->render( template => 'user/master_products_database');
142 }
143  
144 0 ▶sub master_products_database_update {
Subroutine master_products_database_update: not called CC=3 SCAR=24.8
Pod undocumented
145 0 my $self = shift;
146  
147 # Check CSRF token
148 0 ▶ return $self->render(text => 'Bad CSRF token!', status => 403)
Branch: 1 branch
BranchTrueFalseTotal
if $self->validation->csrf_protect->has_error("csrf_token") 0 0 0
149 if $self->validation->csrf_protect->has_error('csrf_token');
150  
151 0 my @ids = map /\d+/g, grep /^id/, @{$self->req->body_params->names};
152  
153 0 for my $id ( @ids ) {
154 0 $self->products->update(
155 $id,
156 map +( $_ => $self->param( $_ . '_' . $id ) ),
157 qw/number image title category group_master
158 group_desc unit description tip_description quote_description recommended price/,
159 );
160 }
161  
162 0 $self->flash( product_update_ok => 1 );
163 0 return $self->redirect_to('/user/master-products-database');
164  
165 }
166  
167 0 ▶sub master_products_database_delete {
Subroutine master_products_database_delete: not called CC=3 SCAR=24.8
Pod undocumented
168 0 my $self = shift;
169  
170 # Check CSRF token
171 0 ▶ return $self->render(text => 'Bad CSRF token!', status => 403)
Branch: 1 branch
BranchTrueFalseTotal
if $self->validation->csrf_protect->has_error("csrf_token") 0 0 0
172 if $self->validation->csrf_protect->has_error('csrf_token');
173  
174 # TODO: implement proper deletion
175 # ... https://github.com/XTaTIK/XTaTIK/issues/122
176 $self->product_search->delete( $_ )
177 0 for $self->products->delete( split ' ', $self->param('to_delete') );
178  
179 0 $self->flash( product_delete_ok => 1 );
180 0 return $self->redirect_to('/user/master-products-database');
181 }
182  
183 0 ▶sub manage_users {
Subroutine manage_users: not called CC=1 SCAR=6.9
Pod undocumented
184 0 my $self = shift;
185  
186 0 $self->stash(
187 users => $self->users->get_all,
188 );
189 }
190  
191 0 ▶sub add_user {
Subroutine add_user: not called CC=2 SCAR=17.9
Pod undocumented
192 0 my $self = shift;
193  
194 $self->form_checker(
195 rules => {
196 login => {
197 max => 3000,
198 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
199 0 ▶ return $self->users->get(shift) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->users->get(shift()) ? : 0 0 0
200 },
201 code_error => 'User with this login already exists',
202 },
203 name => { max => 3000 },
204 email => { max => 3000 },
205 phone => { max => 3000 },
206 roles => {
207 max => 10_000,
208 0 ▶ code => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
209 0 my $v = shift =~ s/^\s+|\s+$//gr;
210 0 for my $role ( split /\s*,\s*/, $v ) {
211 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($role eq $_), $self->users->valid_roles) 0 0 0
212 unless grep $role eq $_,
213 $self->users->valid_roles;
214 }
215 0 return 1;
216 },
217 0 code_error => 'One of the roles you provided is not valid',
218 },
219 },
220 );
221  
222 0 ▶ return $self->render( template => 'user/manage_users' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->form_checker_ok 0 0 0
223 unless $self->form_checker_ok;
224  
225 0 $self->users->add(
226 map +( $_ => $self->param($_) ),
227 qw/login pass name email phone roles/,
228 );
229  
230 0 $self->flash( add_success => 1, );
231 0 $self->redirect_to('/user/manage-users');
232 }
233  
234 0 ▶sub update_users {
Subroutine update_users: not called CC=2 SCAR=17.9
Pod undocumented
235 0 my $self = shift;
236  
237 # TODO: implement parameter checking for each user we're updating
238  
239 0 my @ids = map /\d+/g, grep /^id/, @{$self->req->body_params->names};
240  
241 0 for my $id ( @ids ) {
242 0 $self->users->update(
243 $id,
244 map +( $_ => $self->param( $_ . '_' . $id ) ),
245 qw/login name email phone roles/,
246 );
247 }
248  
249 0 $self->flash( users_update_ok => 1 );
250 0 return $self->redirect_to('/user/manage-users');
251 }
252  
253 0 ▶sub delete_users {
Subroutine delete_users: not called CC=2 SCAR=17.9
Pod undocumented
254 0 my $self = shift;
255 0 $self->users->delete( $_ ) for split ' ', $self->param('to_delete');
256  
257 0 $self->flash( users_delete_ok => 1 );
258 0 return $self->redirect_to('/user/manage-users');
259 }
260  
261 0 ▶sub hot_products {
Subroutine hot_products: not called CC=1 SCAR=6.9
Pod undocumented
262 0 my $self = shift;
263 0 $self->param( hot_products => $self->xvar('hot_products') );
264 }
265  
266 0 ▶sub hot_products_post {
Subroutine hot_products_post: not called CC=1 SCAR=6.9
Pod undocumented
267 0 my $self = shift;
268  
269 0 $self->xvar('hot_products', $self->param('hot_products'));
270 0 $self->stash( update_success => 1, );
271 0 $self->render('user/hot_products');
272 }
273  
274 0 ▶sub quotes_handler {
Subroutine quotes_handler: not called CC=1 SCAR=6.9
Pod undocumented
275 0 my $self = shift;
276 0 $self->stash( quotes => $self->quotes->all );
277 }
278  
279 1 1;
« XTaTIK/Plugin/Cart/PayPal.pm ↑ Summary XTaTIK/Model/Blog.pm »