| 1 |
package Lemonldap::NG::Manager::Api::Menu::Cat; |
| 2 |
|
| 3 |
our $VERSION = '2.23.0'; |
| 4 |
|
| 5 |
package Lemonldap::NG::Manager::Api; |
| 6 |
|
| 7 |
use strict; |
| 8 |
use utf8; |
| 9 |
use Mouse; |
| 10 |
use Lemonldap::NG::Manager::Conf::Parser; |
| 11 |
|
| 12 |
extends 'Lemonldap::NG::Manager::Api::Common'; |
| 13 |
|
| 14 |
sub getMenuCatByConfKey { |
| 15 |
my ( $self, $req ) = @_; |
| 16 |
|
| 17 |
my $confKey = $req->params('confKey') |
| 18 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 19 |
|
| 20 |
$self->logger->debug( |
| 21 |
"[API] Menu Category $confKey configuration requested"); |
| 22 |
|
| 23 |
# Get latest configuration |
| 24 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 25 |
|
| 26 |
my $menuCat = $self->_getMenuCatByConfKey( $conf, $confKey ); |
| 27 |
|
| 28 |
# Return 404 if not found |
| 29 |
return $self->sendError( $req, "Menu category '$confKey' not found", 404 ) |
| 30 |
unless ( defined $menuCat ); |
| 31 |
|
| 32 |
return $self->sendJSONresponse( $req, $menuCat ); |
| 33 |
} |
| 34 |
|
| 35 |
sub findMenuCatByConfKey { |
| 36 |
my ( $self, $req ) = @_; |
| 37 |
|
| 38 |
my $pattern = ( |
| 39 |
defined $req->params('uPattern') |
| 40 |
? $req->params('uPattern') |
| 41 |
: ( defined $req->params('pattern') ? $req->params('pattern') : undef ) |
| 42 |
); |
| 43 |
|
| 44 |
return $self->sendError( $req, 'Invalid input: pattern is missing', 400 ) |
| 45 |
unless ( defined $pattern ); |
| 46 |
|
| 47 |
unless ( $pattern = $self->_getRegexpFromPattern($pattern) ) { |
| 48 |
return $self->sendError( $req, 'Invalid input: pattern is invalid', |
| 49 |
400 ); |
| 50 |
} |
| 51 |
|
| 52 |
$self->logger->debug( |
| 53 |
"[API] Find Menu Categories by confKey regexp $pattern requested"); |
| 54 |
|
| 55 |
# Get latest configuration |
| 56 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 57 |
|
| 58 |
my @menuCats = |
| 59 |
map { $_ =~ $pattern ? $self->_getMenuCatByConfKey( $conf, $_ ) : () } |
| 60 |
keys %{ $conf->{applicationList} }; |
| 61 |
|
| 62 |
return $self->sendJSONresponse( $req, [@menuCats] ); |
| 63 |
} |
| 64 |
|
| 65 |
sub addMenuCat { |
| 66 |
my ( $self, $req ) = @_; |
| 67 |
my $add = $req->jsonBodyToObj; |
| 68 |
|
| 69 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 70 |
unless ($add); |
| 71 |
|
| 72 |
return $self->sendError( $req, 'Invalid input: confKey is missing', 400 ) |
| 73 |
unless ( defined $add->{confKey} ); |
| 74 |
|
| 75 |
return $self->sendError( $req, 'Invalid input: confKey is not a string', |
| 76 |
400 ) |
| 77 |
if ( ref $add->{confKey} ); |
| 78 |
|
| 79 |
return $self->sendError( $req, |
| 80 |
'Invalid input: confKey contains invalid characters', 400 ) |
| 81 |
unless ( $add->{confKey} =~ '^\w[\w\.\-]*$' ); |
| 82 |
|
| 83 |
return $self->sendError( $req, 'Invalid input: catname is missing', 400 ) |
| 84 |
unless ( defined $add->{catname} ); |
| 85 |
|
| 86 |
return $self->sendError( $req, 'Invalid input: catname is not a string', |
| 87 |
400 ) |
| 88 |
if ( ref $add->{catname} ); |
| 89 |
|
| 90 |
$self->logger->debug( |
| 91 |
"[API] Add Menu Category with confKey $add->{confKey} requested"); |
| 92 |
|
| 93 |
# Get latest configuration |
| 94 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 95 |
|
| 96 |
return $self->sendError( |
| 97 |
$req, |
| 98 |
"Invalid input: A Menu Category with confKey $add->{confKey} already exists", |
| 99 |
409 |
| 100 |
) if ( defined $self->_getMenuCatByConfKey( $conf, $add->{confKey} ) ); |
| 101 |
|
| 102 |
my $res = $self->_pushMenuCat( $conf, $add->{confKey}, $add, 1 ); |
| 103 |
|
| 104 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 105 |
unless ( $res->{res} eq 'ok' ); |
| 106 |
|
| 107 |
return $self->sendJSONresponse( |
| 108 |
$req, |
| 109 |
{ message => "Successful operation" }, |
| 110 |
code => 201 |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
sub updateMenuCat { |
| 115 |
my ( $self, $req ) = @_; |
| 116 |
my $confKey = $req->params('confKey') |
| 117 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 118 |
|
| 119 |
my $update = $req->jsonBodyToObj; |
| 120 |
|
| 121 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 122 |
unless ($update); |
| 123 |
|
| 124 |
$self->logger->debug( |
| 125 |
"[API] Menu Category $confKey configuration update requested"); |
| 126 |
|
| 127 |
# Get latest configuration |
| 128 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 129 |
|
| 130 |
my $current = $self->_getMenuCatByConfKey( $conf, $confKey ); |
| 131 |
|
| 132 |
# Return 404 if not found |
| 133 |
|
| 134 |
return $self->sendError( $req, "Menu category '$confKey' not found", 404 ) |
| 135 |
unless ( defined $current ); |
| 136 |
|
| 137 |
my $res = $self->_pushMenuCat( $conf, $confKey, $update, 0 ); |
| 138 |
|
| 139 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 140 |
unless ( $res->{res} eq 'ok' ); |
| 141 |
|
| 142 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 143 |
} |
| 144 |
|
| 145 |
sub replaceMenuCat { |
| 146 |
my ( $self, $req ) = @_; |
| 147 |
my $confKey = $req->params('confKey') |
| 148 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 149 |
|
| 150 |
my $replace = $req->jsonBodyToObj; |
| 151 |
|
| 152 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 153 |
unless ($replace); |
| 154 |
|
| 155 |
return $self->sendError( $req, 'Invalid input: confKey is missing', 400 ) |
| 156 |
unless ( defined $replace->{confKey} ); |
| 157 |
|
| 158 |
return $self->sendError( $req, 'Invalid input: confKey is not a string', |
| 159 |
400 ) |
| 160 |
if ( ref $replace->{confKey} ); |
| 161 |
|
| 162 |
return $self->sendError( $req, |
| 163 |
'Invalid input: confKey contains invalid characters', 400 ) |
| 164 |
unless ( $replace->{confKey} =~ '^\w[\w\.\-]*$' ); |
| 165 |
|
| 166 |
return $self->sendError( $req, 'Invalid input: catname is missing', 400 ) |
| 167 |
unless ( defined $replace->{catname} ); |
| 168 |
|
| 169 |
return $self->sendError( $req, 'Invalid input: catname is not a string', |
| 170 |
400 ) |
| 171 |
if ( ref $replace->{catname} ); |
| 172 |
|
| 173 |
$self->logger->debug( |
| 174 |
"[API] Menu Category $confKey configuration replace requested"); |
| 175 |
|
| 176 |
# Get latest configuration |
| 177 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 178 |
|
| 179 |
# Return 404 if not found |
| 180 |
|
| 181 |
return $self->sendError( $req, "Menu category '$confKey' not found", 404 ) |
| 182 |
unless ( defined $self->_getMenuCatByConfKey( $conf, $confKey ) ); |
| 183 |
|
| 184 |
my $res = $self->_pushMenuCat( $conf, $confKey, $replace, 1 ); |
| 185 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 186 |
unless ( $res->{res} eq 'ok' ); |
| 187 |
|
| 188 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 189 |
} |
| 190 |
|
| 191 |
sub deleteMenuCat { |
| 192 |
my ( $self, $req ) = @_; |
| 193 |
my $confKey = $req->params('confKey') |
| 194 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 195 |
|
| 196 |
$self->logger->debug( |
| 197 |
"[API] Menu Category $confKey configuration delete requested"); |
| 198 |
|
| 199 |
# Get latest configuration |
| 200 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 201 |
|
| 202 |
my $delete = $self->_getMenuCatByConfKey( $conf, $confKey ); |
| 203 |
|
| 204 |
# Return 404 if not found |
| 205 |
|
| 206 |
return $self->sendError( $req, "Menu category '$confKey' not found", 404 ) |
| 207 |
unless ( defined $delete ); |
| 208 |
|
| 209 |
delete $conf->{applicationList}->{$confKey}; |
| 210 |
|
| 211 |
# Save configuration |
| 212 |
$self->_saveApplyConf($conf); |
| 213 |
|
| 214 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 215 |
} |
| 216 |
|
| 217 |
sub _getMenuCatByConfKey { |
| 218 |
my ( $self, $conf, $confKey ) = @_; |
| 219 |
|
| 220 |
# Check if confKey is defined |
| 221 |
return undef unless ( defined $conf->{applicationList}->{$confKey} ); |
| 222 |
|
| 223 |
my $menuCat = { |
| 224 |
confKey => $confKey, |
| 225 |
catname => $conf->{applicationList}->{$confKey}->{catname} |
| 226 |
}; |
| 227 |
|
| 228 |
$menuCat->{order} = $conf->{applicationList}->{$confKey}->{order} |
| 229 |
if ( defined $conf->{applicationList}->{$confKey}->{order} ); |
| 230 |
|
| 231 |
return $menuCat; |
| 232 |
} |
| 233 |
|
| 234 |
sub _pushMenuCat { |
| 235 |
my ( $self, $conf, $confKey, $push, $replace ) = @_; |
| 236 |
|
| 237 |
if ($replace) { |
| 238 |
$conf->{applicationList}->{$confKey} = {}; |
| 239 |
$conf->{applicationList}->{$confKey}->{type} = "category"; |
| 240 |
} |
| 241 |
|
| 242 |
$conf->{applicationList}->{$confKey}->{order} = $push->{order} |
| 243 |
if ( defined $push->{order} ); |
| 244 |
|
| 245 |
$conf->{applicationList}->{$confKey}->{catname} = $push->{catname} |
| 246 |
if ( defined $push->{catname} ); |
| 247 |
|
| 248 |
# Test new configuration |
| 249 |
my $parser = Lemonldap::NG::Manager::Conf::Parser->new( { |
| 250 |
refConf => $self->_confAcc->getConf( { noCache => 1 } ), |
| 251 |
newConf => $conf, |
| 252 |
req => {}, |
| 253 |
} |
| 254 |
); |
| 255 |
unless ( $parser->testNewConf( $self->p ) ) { |
| 256 |
return { |
| 257 |
res => 'ko', |
| 258 |
code => 400, |
| 259 |
msg => "Configuration error: " |
| 260 |
. join( ". ", map { $_->{message} } @{ $parser->errors } ), |
| 261 |
}; |
| 262 |
} |
| 263 |
|
| 264 |
# Save configuration |
| 265 |
$self->_saveApplyConf($conf); |
| 266 |
|
| 267 |
return { res => 'ok' }; |
| 268 |
} |
| 269 |
|
| 270 |
1; |