Lemonldap-NG-Manager

 view release on metacpan or  search on metacpan

lib/Lemonldap/NG/Manager/Api/Menu/Cat.pm  view on Meta::CPAN


    # Get latest configuration
    my $conf = $self->_confAcc->getConf;

    my @menuCats =
      map { $_ =~ $pattern ? $self->_getMenuCatByConfKey( $conf, $_ ) : () }
      keys %{ $conf->{applicationList} };

    return $self->sendJSONresponse( $req, [@menuCats] );
}

sub addMenuCat {
    my ( $self, $req ) = @_;
    my $add = $req->jsonBodyToObj;

    return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
      unless ($add);

    return $self->sendError( $req, 'Invalid input: confKey is missing', 400 )
      unless ( defined $add->{confKey} );

    return $self->sendError( $req, 'Invalid input: confKey is not a string',
        400 )
      if ( ref $add->{confKey} );

    return $self->sendError( $req,
        'Invalid input: confKey contains invalid characters', 400 )
      unless ( $add->{confKey} =~ '^\w[\w\.\-]*$' );

    return $self->sendError( $req, 'Invalid input: catname is missing', 400 )
      unless ( defined $add->{catname} );

    return $self->sendError( $req, 'Invalid input: catname is not a string',
        400 )
      if ( ref $add->{catname} );

    $self->logger->debug(
        "[API] Add Menu Category with confKey $add->{confKey} requested");

    # Get latest configuration
    my $conf = $self->_confAcc->getConf( { noCache => 1 } );

    return $self->sendError(
        $req,
"Invalid input: A Menu Category with confKey $add->{confKey} already exists",
        409
    ) if ( defined $self->_getMenuCatByConfKey( $conf, $add->{confKey} ) );

    my $res = $self->_pushMenuCat( $conf, $add->{confKey}, $add, 1 );

    return $self->sendError( $req, $res->{msg}, $res->{code} || 400 )
      unless ( $res->{res} eq 'ok' );

    return $self->sendJSONresponse(
        $req,
        { message => "Successful operation" },
        code => 201
    );
}

sub updateMenuCat {
    my ( $self, $req ) = @_;
    my $confKey = $req->params('confKey')
      or return $self->sendError( $req, 'confKey is missing', 400 );

    my $update = $req->jsonBodyToObj;

    return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
      unless ($update);

    $self->logger->debug(
        "[API] Menu Category $confKey configuration update requested");

    # Get latest configuration
    my $conf = $self->_confAcc->getConf( { noCache => 1 } );

    my $current = $self->_getMenuCatByConfKey( $conf, $confKey );

    # Return 404 if not found

    return $self->sendError( $req, "Menu category '$confKey' not found", 404 )
      unless ( defined $current );

    my $res = $self->_pushMenuCat( $conf, $confKey, $update, 0 );

    return $self->sendError( $req, $res->{msg}, $res->{code} || 400 )
      unless ( $res->{res} eq 'ok' );

    return $self->sendJSONresponse( $req, undef, code => 204 );
}

sub replaceMenuCat {
    my ( $self, $req ) = @_;
    my $confKey = $req->params('confKey')
      or return $self->sendError( $req, 'confKey is missing', 400 );

    my $replace = $req->jsonBodyToObj;

    return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
      unless ($replace);

    return $self->sendError( $req, 'Invalid input: confKey is missing', 400 )
      unless ( defined $replace->{confKey} );

    return $self->sendError( $req, 'Invalid input: confKey is not a string',
        400 )
      if ( ref $replace->{confKey} );

    return $self->sendError( $req,
        'Invalid input: confKey contains invalid characters', 400 )
      unless ( $replace->{confKey} =~ '^\w[\w\.\-]*$' );

    return $self->sendError( $req, 'Invalid input: catname is missing', 400 )
      unless ( defined $replace->{catname} );

    return $self->sendError( $req, 'Invalid input: catname is not a string',
        400 )
      if ( ref $replace->{catname} );

    $self->logger->debug(
        "[API] Menu Category $confKey configuration replace requested");



( run in 2.555 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )