| 1 | package Rapi::Blog::Module::PageBase; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | extends 'RapidApp::Module::DbicPropPage'; | ||
| 8 | |||
| 9 | use RapidApp::Util ':all'; | ||
| 10 | use Rapi::Blog::Util; | ||
| 11 | |||
| 12 | |||
| 13 | before 'content' => sub { (shift)->apply_permissions }; | ||
| 14 | |||
| 15 | sub apply_permissions { | ||
| 16 | my $self = shift; | ||
| 17 | my $c = RapidApp->active_request_context or return; | ||
| 18 | |||
| 19 | # System 'administrator' role trumps everything: | ||
| 20 | return if ($c->check_user_roles('administrator')); | ||
| 21 | |||
| 22 | my $uid = Rapi::Blog::Util->get_uid or return $self->_perm_deny_all_changes; | ||
| 23 | my $reqRow = $self->req_Row or return $self->_perm_deny_all_changes; | ||
| 24 | |||
| 25 | my $source_name = $self->ResultSource->source_name; | ||
| 26 | |||
| 27 | # Note: 'Post' has its own module so isn't listed here | ||
| 28 | if($source_name eq 'Comment') { | ||
| 29 | if($reqRow->user_id == $uid) { | ||
| 30 | # Allow user to edit their own comments but only for these columns: | ||
| 31 | my @edit_cols = qw/body/; | ||
| 32 | $self->apply_except_colspec_columns(\@edit_cols, | ||
| 33 | allow_add => 0, | ||
| 34 | allow_edit => 0 | ||
| 35 | ); | ||
| 36 | } | ||
| 37 | else { | ||
| 38 | # Deny all changes except to the user's own comments: | ||
| 39 | $self->_perm_deny_all_changes; | ||
| 40 | } | ||
| 41 | |||
| 42 | } | ||
| 43 | elsif($source_name eq 'User') { | ||
| 44 | if($reqRow->id == $uid) { | ||
| 45 | # Allow user to edit their own account but only for these columns: | ||
| 46 | my @edit_cols = qw/username full_name set_pw/; | ||
| 47 | $self->apply_except_colspec_columns(\@edit_cols, | ||
| 48 | allow_add => 0, | ||
| 49 | allow_edit => 0 | ||
| 50 | ); | ||
| 51 | |||
| 52 | # We never want them to see preauth_actions: | ||
| 53 | $self->apply_colspec_columns(['preauth_actions'], no_column => 1); | ||
| 54 | |||
| 55 | |||
| 56 | } | ||
| 57 | else { | ||
| 58 | # Deny all changes except to the user's own account: | ||
| 59 | $self->_perm_deny_all_changes; | ||
| 60 | |||
| 61 | # And deny | ||
| 62 | my @deny_columns = qw/email admin author comment disabled set_pw preauth_actions/; | ||
| 63 | $self->apply_colspec_columns(\@deny_columns, no_column => 1); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | # Deny all changes unless otherwise specified | ||
| 68 | $self->_perm_deny_all_changes; | ||
| 69 | } | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub _perm_deny_all_changes { | ||
| 75 | my $self = shift; | ||
| 76 | $self->apply_to_all_columns( allow_add => 0, allow_edit => 0 ); | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 |