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