| | |
| | | <?php |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\Instance; |
| | | use JVBase\managers\InvitationsManager; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\managers\CustomTable; |
| | | use JVBase\rest\Route; |
| | |
| | | */ |
| | | class Invitations extends Rest |
| | | { |
| | | use Instance; |
| | | protected array $inviteConfig; |
| | | protected CustomTable $table; |
| | | |
| | |
| | | parent::__construct(); |
| | | |
| | | // Get invitation configuration |
| | | $this->inviteConfig = JVB()->invitations()->getInviteConfig(); |
| | | $this->inviteConfig = InvitationsManager::getInstance()->getInviteConfig(); |
| | | $this->table = CustomTable::for('invitations'); |
| | | |
| | | // Cache connections |
| | |
| | | Route::for('invitations') |
| | | ->get([$this, 'getInvitations']) |
| | | ->args([ |
| | | 'user' => 'int|required', |
| | | 'to_term' => 'int', |
| | | 'taxonomy' => 'string', |
| | | 'status' => 'string|enum:all,pending,accepted,rejected,expired,revoked|default:all', |
| | |
| | | |
| | | ->post([$this, 'createInvitationRequest']) |
| | | ->args([ |
| | | 'user' => 'int|required', |
| | | 'action' => 'string|enum:create,revoke,refresh|default:create', |
| | | 'invites' => 'array', |
| | | 'invitation_id' => 'int' |
| | | ]) |
| | | ->auth('verified') |
| | | ->rateLimit(10, 300); |
| | | ->rateLimit(10, 300) |
| | | ->register(); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public function getInvitations(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $userID = $request->get_param('user'); |
| | | $userID = get_current_user_id(); |
| | | $termID = $request->get_param('to_term'); |
| | | $taxonomy = $request->get_param('taxonomy'); |
| | | |
| | | // Validate user |
| | | if (get_current_user_id() !== $userID) { |
| | | if (!$userID) { |
| | | return $this->unauthorized('Invalid user'); |
| | | } |
| | | |
| | |
| | | ]; |
| | | |
| | | // Check cache |
| | | $key = $this->cache->generateKey($args); |
| | | if ($cached = $this->cache->get($key)) { |
| | | |
| | | if ($cached = $this->cache->get($args)) { |
| | | return $this->success($cached); |
| | | } |
| | | |
| | |
| | | : $this->getUserInvitations($args); |
| | | |
| | | // Cache result |
| | | $this->cache->set($key, $result); |
| | | $this->cache->set($args, $result); |
| | | |
| | | return $this->success($result); |
| | | } |
| | |
| | | /** |
| | | * Get invitations for a specific term |
| | | */ |
| | | protected function getTermInvitations(array $args): array |
| | | public function getTermInvitations(array $args): array |
| | | { |
| | | // Check permission |
| | | if (!JVB()->roles()->isManager($args['user'], $args['to_term'])) { |
| | | if (!Roles::getInstance()()->isManager($args['user'], $args['to_term'])) { |
| | | return $this->forbidden('You cannot view invitations for this ' . $args['taxonomy'])->get_data(); |
| | | } |
| | | |
| | |
| | | */ |
| | | public function createInvitationRequest(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $userID = $request->get_param('user'); |
| | | $userID = get_current_user_id(); |
| | | $action = $request->get_param('action'); |
| | | |
| | | // Validate user |
| | | if (get_current_user_id() !== $userID) { |
| | | if (!$userID) { |
| | | return $this->unauthorized('Invalid user'); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | // Queue using fluent interface |
| | | $op = JVB()->queue()->add( |
| | | $op = Queue::getInstance()->add( |
| | | 'invitation_create', |
| | | $userID, |
| | | ['invitations' => $validated], |
| | |
| | | */ |
| | | protected function validateInvitations(int $userID, array $invites): array |
| | | { |
| | | return JVB()->invitations()->validateInvitations($userID, $invites); |
| | | return InvitationsManager::getInstance()->validateInvitations($userID, $invites); |
| | | } |
| | | |
| | | |
| | |
| | | return $this->error('Invitation ID required'); |
| | | } |
| | | |
| | | $op = JVB()->queue()->add( |
| | | $op = Queue::getInstance()->add( |
| | | 'invitation_revoke', |
| | | $userID, |
| | | ['invitation_id' => $invitationID], |
| | |
| | | return $this->error('Invitation ID required'); |
| | | } |
| | | |
| | | $op = JVB()->queue()->add( |
| | | $op = Queue::getInstance()->add( |
| | | 'invitation_resend', |
| | | $userID, |
| | | ['invitation_id' => $invitationID], |
| | |
| | | ]; |
| | | |
| | | // Add term information if present |
| | | foreach (JVB()->roles()->getInvitableTaxonomies() as $taxonomy) { |
| | | foreach (Roles::getInstance()()->getInvitableTaxonomies() as $taxonomy) { |
| | | $column = 'to_' . $taxonomy; |
| | | if (isset($invitation->$column) && $invitation->$column) { |
| | | $termID = (int) $invitation->$column; |