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