Open Journal Systems  3.3.0
PKPStatsUserHandler.inc.php
1 <?php
2 
17 import('lib.pkp.classes.handler.APIHandler');
18 import('classes.core.Services');
19 
21 
25  public function __construct() {
26  $this->_handlerPath = 'stats/users';
27  $this->_endpoints = [
28  'GET' => [
29  [
30  'pattern' => $this->getEndpointPattern(),
31  'handler' => [$this, 'get'],
32  'roles' => [ROLE_ID_SITE_ADMIN, ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR],
33  ],
34  ],
35  ];
36  parent::__construct();
37  }
38 
42  function authorize($request, &$args, $roleAssignments) {
43 
44  import('lib.pkp.classes.security.authorization.ContextAccessPolicy');
45  $this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
46 
47  import('lib.pkp.classes.security.authorization.PolicySet');
48  $rolePolicy = new PolicySet(COMBINING_PERMIT_OVERRIDES);
49  import('lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
50  foreach ($roleAssignments as $role => $operations) {
51  $rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations));
52  }
53  $this->addPolicy($rolePolicy);
54 
55  return parent::authorize($request, $args, $roleAssignments);
56  }
57 
68  public function get($slimRequest, $response, $args) {
69  $request = $this->getRequest();
70 
71  if (!$request->getContext()) {
72  return $response->withStatus(404)->withJsonError('api.404.resourceNotFound');
73  }
74 
75  $defaultParams = [
76  'status' => 'active'
77  ];
78 
79  $params = [];
80  foreach ($slimRequest->getQueryParams() as $param => $value) {
81  switch ($param) {
82  case 'registeredAfter':
83  case 'registeredBefore':
84  $params[$param] = $value;
85  break;
86 
87  case 'status':
88  $params[$param] = $value === 'disabled' ? $value : 'active';
89  break;
90  }
91  }
92 
93  $params = array_merge($defaultParams, $params);
94 
95  \HookRegistry::call('API::stats::users::params', array(&$params, $slimRequest));
96 
97  $params['contextId'] = [$request->getContext()->getId()];
98 
99  $result = $this->_validateStatDates($params, 'registeredAfter', 'registeredBefore');
100  if ($result !== true) {
101  return $response->withStatus(400)->withJsonError($result);
102  }
103 
104  return $response->withJson(array_map(
105  function ($item) {
106  $item['name'] = __($item['name']);
107  return $item;
108  },
109  Services::get('user')->getRolesOverview($params)
110  ));
111  }
112 }
PKPStatsUserHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: PKPStatsUserHandler.inc.php:42
ContextAccessPolicy
Class to control access to PKP applications' setup components.
Definition: ContextAccessPolicy.inc.php:17
APIHandler\_validateStatDates
_validateStatDates($params, $dateStartParam='dateStart', $dateEndParam='dateEnd')
Definition: APIHandler.inc.php:396
PKPStatsUserHandler\__construct
__construct()
Definition: PKPStatsUserHandler.inc.php:25
APIHandler
Base request API handler.
Definition: APIHandler.inc.php:22
RoleBasedHandlerOperationPolicy
Class to control access to handler operations via role based access control.
Definition: RoleBasedHandlerOperationPolicy.inc.php:18
APIHandler\getRequest
getRequest()
Definition: APIHandler.inc.php:149
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
APIHandler\getEndpointPattern
getEndpointPattern()
Definition: APIHandler.inc.php:186
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
PolicySet
An ordered list of policies. Policy sets can be added to decision managers like policies....
Definition: PolicySet.inc.php:26
PKPStatsUserHandler
Handle API requests for publication statistics.
Definition: PKPStatsUserHandler.inc.php:20
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49