| 1 | package plenigo::AccessRightsManager; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | plenigo::AccessRightsManager - A utility class to get/add/remove access rights from/to a customer. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use plenigo::AccessRightsManager; | ||
| 10 | |||
| 11 | # Prepare configuration | ||
| 12 | |||
| 13 | my $use_stage = 0; # set if stage system should be used | ||
| 14 | my $configuration = plenigo::Configuration->new(access_token => 'ACCESS_TOKEN', use_stage => $use_stage); | ||
| 15 | |||
| 16 | # Instantiate access rights manager | ||
| 17 | |||
| 18 | my $access_rights = plenigo::AccessRightsManager->new(configuration => $configuration); | ||
| 19 | |||
| 20 | # Checking access rights from a customer | ||
| 21 | |||
| 22 | my %access_rights = $access_rights->hasAccess($plenigo_customer_id, ['ACCESS_RIGHT_ID']); | ||
| 23 | if ($access_rights{'accessGranted'}) { | ||
| 24 | # customer has the right to access | ||
| 25 | } | ||
| 26 | else { | ||
| 27 | # customer is not allowed to access | ||
| 28 | } | ||
| 29 | |||
| 30 | # Adding access rights to a customer | ||
| 31 | |||
| 32 | $access_rights->addAccess($plenigo_customer_id, (details => [{productId => 'ACCESS_RIGHT_ID'}])); | ||
| 33 | |||
| 34 | # Removing access rights from a customer | ||
| 35 | |||
| 36 | $access_rights->removeAccess($plenigo_customer_id, ['ACCESS_RIGHT_ID']); | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | plenigo::AccessRightsManager provides functionality to manage access rights of a customer. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | use Moo; | ||
| 45 | use Carp qw(confess); | ||
| 46 | use plenigo::Ex; | ||
| 47 | use plenigo::RestClient; | ||
| 48 | |||
| 49 | our $VERSION = '3.0002'; | ||
| 50 | |||
| 51 | has configuration => ( | ||
| 52 | is => 'ro', | ||
| 53 | required => 1, | ||
| 54 | ); | ||
| 55 | |||
| 56 | has _rest_client => (is => 'lazy',); | ||
| 57 | |||
| 58 | sub _build__rest_client { | ||
| 59 | my $self = shift; | ||
| 60 | my $rest_client = plenigo::RestClient->new(configuration => $self->configuration); | ||
| 61 | return $rest_client; | ||
| 62 | } | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub getAccessRights { | ||
| 69 | my ($self, $customer_id) = @_; | ||
| 70 | |||
| 71 | my %result = $self->_rest_client->get("accessRights/$customer_id"); | ||
| 72 | |||
| 73 | my @access_right_unique_ids = map { | ||
| 74 | $_->{accessRightUniqueId} | ||
| 75 | } @{$result{response_content}->{items}}; | ||
| 76 | |||
| 77 | return $self->hasAccess($customer_id, @access_right_unique_ids); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub getGrantedAccessRightsItems { | ||
| 81 | my ($self, $customer_id) = @_; | ||
| 82 | |||
| 83 | my %access_rights = $self->getAccessRights($customer_id); | ||
| 84 | |||
| 85 | return [ | ||
| 86 | grep { | ||
| 87 | $_->{accessGranted} | ||
| 88 | } @{$access_rights{items}} | ||
| 89 | ]; | ||
| 90 | } | ||
| 91 | |||
| 92 | =head2 hasAccess($customer_id, @product_ids) | ||
| 93 | |||
| 94 | Test if a customer has access rights. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub hasAccess { | ||
| 99 | my ($self, $customer_id, @access_right_unique_ids) = @_; | ||
| 100 | |||
| 101 | my $rest_client = plenigo::RestClient->new(configuration => $self->configuration); | ||
| 102 | my %result = $rest_client->get( | ||
| 103 | 'accessRights/' . $customer_id . '/hasAccess', | ||
| 104 | { accessRightUniqueIds => join(',', @access_right_unique_ids) } | ||
| 105 | ); | ||
| 106 | if ($result{'response_code'} == 403) { | ||
| 107 | return('accessGranted' => 0) | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | return %{$result{'response_content'}}; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | =head2 addAccess($customer_id, %access_request) | ||
| 115 | |||
| 116 | Add access rights to a customer. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub addAccess { | ||
| 121 | my ($self, $customer_id, %access_request) = @_; | ||
| 122 | |||
| 123 | my $rest_client = plenigo::RestClient->new(configuration => $self->configuration); | ||
| 124 | my $result = $rest_client->post('access/' . $customer_id . '/addWithDetails', {}, %access_request); | ||
| 125 | if (defined $result && $result->response_code == 404) { | ||
| 126 | plenigo::Ex->throw({ code => 404, message => 'There is no customer for the customer id passed.' }); | ||
| 127 | } | ||
| 128 | |||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | |||
| 132 | =head2 removeAccess($customer_id, $access_right_unique_id) | ||
| 133 | |||
| 134 | Remove access right from a customer. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub removeAccess { | ||
| 139 | my ($self, $customer_id, $access_right_unique_id) = @_; | ||
| 140 | |||
| 141 | my $rest_client = plenigo::RestClient->new(configuration => $self->configuration); | ||
| 142 | $rest_client->delete('accessRights/' . $customer_id . '/', $access_right_unique_id); | ||
| 143 | |||
| 144 | return 1; | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; |