MojoMojo

 view release on metacpan or  search on metacpan

lib/MojoMojo/Controller/Jsrpc.pm  view on Meta::CPAN


=cut

sub untag : Local Args(1) {
    my ( $self, $c, $tagname ) = @_;
    my $page = $c->stash->{page};
    die "Page " . $page . " not found" unless ref $page;
    my $tag = $c->model("DBIC::Tag")->search(
      {
        page   => $page->id,
        person => $c->user->obj->id,
        tag    => $tagname
      }
    )->next();
    $tag->delete() if $tag;
    $c->req->args( [$tagname] );
    $c->forward('/page/inline_tags');
}

=head2 imginfo ( .jsrpc/imginfo )

Inline info on hover for gallery photos.

=cut

sub imginfo : Local {
    my ( $self, $c, $photo ) = @_;
    $c->stash->{photo}    = $c->model("DBIC::Photo")->find($photo);
    $c->stash->{template} = 'gallery/imginfo.tt';
}

=head2 usersearch ( .jsrpc/usersearch )

Backend that handles jQuery autocomplete requests for users.

=cut

sub usersearch : Local {
    my ($self, $c) = @_;
    my $query = $c->req->param('q');

    $c->stash->{template} = "user/user_search.tt";

    if (defined($query) && length($query)) {
        my $rs = $c->model('DBIC::Person')->search({
            login => { -like => '%'.$query.'%'}
        });
        $c->stash->{users} = [ $rs->all ];
    }
}

=head2 set_permissions ( .jsrpc/set_permissions )

Sets page permissions.

=cut

sub set_permissions : Local {
    my ($self, $c) = @_;

    $c->forward('validate_perm_edit');

    my @path_elements = $c->_expand_path_elements($c->stash->{path});
    my $current_path = pop @path_elements;

    my ( $create, $read, $write, $delete, $attachment, $subpages) =
        map { $c->req->param($_) ? 'yes' : 'no' }
            qw/create read write delete attachment subpages/;

    my $role = $c->stash->{role};

    my $params = {
        path => $current_path,
        role => $role->id,
        apply_to_subpages   => $subpages,
        create_allowed      => $create,
        delete_allowed      => $delete,
        edit_allowed        => $write,
        view_allowed        => $read,
        attachment_allowed  => $attachment
    };

    my $model = $c->model('DBIC::PathPermissions');

    # when subpages should inherit permissions we actually need to update two
    # entries: one for the subpages and one for the current page
    if ($subpages eq 'yes') {
        # update permissions for subpages
        $model->update_or_create( $params );

        # update permissions for the current page
        $params->{apply_to_subpages} = 'no';
        $model->update_or_create( $params );
    }
    # otherwise, we must remove the subpages permissions entry and update the
    # entry for the current page
    else {
        # delete permissions for subpages
        $model->search( {
            path              => $current_path,
            role              => $role->id,
            apply_to_subpages => 'yes'
        } )->delete;

        # update permissions for the current page
        $model->update_or_create($params);
    }

    # clear cache
    if ( $c->pref('cache_permission_data') ) {
        $c->cache->remove( 'page_permission_data' );
    }

    $c->res->body("OK");
    $c->res->status(200);
}

=head2 clear_permissions ( .jsrpc/clear_permissions )

Clears this page permissions for a given role (making permissions inherited).

=cut

sub clear_permissions : Local {
    my ($self, $c) = @_;

    $c->forward('validate_perm_edit');

    my @path_elements = $c->_expand_path_elements($c->stash->{path});
    my $current_path = pop @path_elements;

    my $role = $c->stash->{role};

    if ($role) {

        # delete permissions for subpages
        $c->model('DBIC::PathPermissions')->search( {
            path              => $current_path,
            role              => $role->id
        } )->delete;

        # clear cache
        if ( $c->pref('cache_permission_data') ) {
            $c->cache->remove( 'page_permission_data' );
        }

    }

    $c->res->body("OK");
    $c->res->status(200);

}

=head2 validate_perm_edit

Validates if the user is able to edit permissions and if a role was supplied.

=cut

sub validate_perm_edit : Private {
    my ($self, $c) = @_;

    my $user = $c->user;

    # only admins can change permissions for now
    unless ($user && $user->is_admin) {
        $c->res->body("Forbidden");
        $c->res->status(403);
        $c->detach;
    }

    my $role = $c->model('DBIC::Role')->find(
        { name => $c->req->param('role_name') }
    );

    unless ($role) {
        $c->res->body('Bad Request');
        $c->res->status(400);
        $c->detach;
    }

    $c->stash->{role} = $role;
}

=head1 AUTHOR

Marcus Ramberg <mramberg@cpan.org>, David Naughton <naughton@cpan.org>

=head1 LICENSE

This library is free software. You can redistribute it and/or modify it under
the same terms as Perl itself.

=cut

1;



( run in 1.161 second using v1.01-cache-2.11-cpan-39bf76dae61 )