Skip to content

Commit

Permalink
Merge pull request #6075 from inverse-inc/feature/tenant-modify-only-…
Browse files Browse the repository at this point in the history
…by-global-tenant

feature/tenant-modify-only-by-global-tenant
  • Loading branch information
nqb committed Mar 16, 2021
2 parents 948ca9a + 40754ea commit 39c7281
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/pf/UnifiedApi/Controller/Crud.pm
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,20 @@ sub build_item_lookup {

sub create {
my ($self) = @_;
my ($status, $msg) = $self->can_create();
if (is_error($status)) {
return $self->render_error($status, $msg);
}

return $self->render_create(
$self->do_create($self->make_create_data())
);
}

sub can_create {
return (200, '');
}

=head2 id
Get id of current resource
Expand Down Expand Up @@ -381,10 +390,15 @@ update

sub update {
my ($self) = @_;
my ($status, $msg) = $self->can_update();
if (is_error($status)) {
return $self->render_error($status, $msg);
}

my $req = $self->req;
my $res = $self->res;
my $data = $self->update_data;
my ($status, $err) = $self->validate($data);
($status, my $err) = $self->validate($data);
if (is_error($status)) {
return $self->render_error(
$status,
Expand Down Expand Up @@ -412,6 +426,16 @@ sub update {
return $self->render(json => { message => "'$id' updated" });
}

=head2 can_update
can update
=cut

sub can_update {
return (200, '');
}

=head2 post_update
post_update
Expand Down
33 changes: 33 additions & 0 deletions lib/pf/UnifiedApi/Controller/Tenants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,44 @@ use strict;
use warnings;
use Mojo::Base 'pf::UnifiedApi::Controller::Crud';
use pf::dal::tenant;
use pf::config::tenant;
use pf::constants qw($DEFAULT_TENANT_ID);

has dal => 'pf::dal::tenant';
has url_param_name => 'tenant_id';
has primary_key => 'id';

sub can_remove {
my ($self) = @_;
if ($self->is_readonly) {
return (403, 'Cannot remove this resource');
}

return $self->SUPER::can_remove;
}

sub can_update {
my ($self) = @_;
if ($self->is_readonly) {
return (403, 'Cannot update this resource');
}

return $self->SUPER::can_update;
}

sub can_create {
my ($self) = @_;
if ($self->is_readonly) {
return (403, 'Cannot create this resource');
}

return $self->SUPER::can_create;
}

sub is_readonly {
pf::config::tenant::get_tenant() > $DEFAULT_TENANT_ID
}

=head1 AUTHOR
Inverse inc. <info@inverse.ca>
Expand Down

0 comments on commit 39c7281

Please sign in to comment.