| @@ -5,19 +5,14 @@ | ||
| 5 | 5 | use FluentSupport\App\Models\Agent; |
| 6 | 6 | use FluentSupport\App\Modules\StatModule; |
| 7 | 7 | use FluentSupport\App\Services\AvatarUploder; |
| 8 | 8 | use FluentSupport\App\Services\Helper; |
| 9 | -use FluentSupport\Framework\Request\Request; | |
| 9 | +use FluentSupport\Framework\Http\Request\Request; | |
| 10 | 10 | use FluentSupport\App\Modules\PermissionManager; |
| 11 | 11 | use FluentSupport\App\Http\Requests\AgentCreateRequest; |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | - * AgentController class for REST API | |
| 15 | - * This class is responsible for getting data for all request related to agent | |
| 16 | - * | |
| 17 | - * @package FluentSupport\App\Http\Controllers | |
| 18 | - * | |
| 19 | - * @version 1.0.0 | |
| 14 | + * Support staff (agent) endpoints. | |
| 20 | 15 | */ |
| 21 | 16 | class AgentController extends Controller |
| 22 | 17 | { |
| 23 | 18 | public function index(Request $request, Agent $agent) |
| @@ -22,16 +17,15 @@ | ||
| 22 | 17 | { |
| 23 | 18 | public function index(Request $request, Agent $agent) |
| 24 | 19 | { |
| 25 | 20 | return [ |
| 26 | - 'agents' => $agent->getAgents($request->getSafe('search','sanitize_text_field')), | |
| 27 | - 'permissions' => PermissionManager::getReadablePermissionGroups(), | |
| 28 | - 'businessBoxes' => PermissionManager::getBusinessBoxesForRestriction(), | |
| 21 | + 'agents' => $agent->getAgents($request->getSafe('search', 'sanitize_text_field')), | |
| 22 | + 'permissions' => PermissionManager::getReadablePermissionGroups(), | |
| 23 | + 'businessBoxes' => PermissionManager::getMailboxesForRestriction(), | |
| 29 | 24 | ]; |
| 30 | 25 | } |
| 31 | 26 | |
| 32 | 27 | /** |
| 33 | - * addAgent method will add new agent in person table | |
| 34 | 28 | * @param AgentCreateRequest $request |
| 35 | 29 | * @return \WP_REST_Response | array |
| 36 | 30 | * @throws \FluentSupport\Framework\Validator\ValidationException |
| 37 | 31 | */ |
| @@ -36,16 +30,9 @@ | ||
| 36 | 30 | * @throws \FluentSupport\Framework\Validator\ValidationException |
| 37 | 31 | */ |
| 38 | 32 | public function addAgent(AgentCreateRequest $request, Agent $agent) |
| 39 | 33 | { |
| 40 | - $data = [ | |
| 41 | - 'email' => $request->getSafe('email', 'sanitize_email'), | |
| 42 | - 'first_name' => $request->getSafe('first_name', 'sanitize_text_field'), | |
| 43 | - 'last_name' => $request->getSafe('last_name', 'sanitize_text_field'), | |
| 44 | - 'title' => $request->getSafe('title', 'sanitize_text_field'), | |
| 45 | - 'permissions' => $request->getSafe('permissions', null, []), | |
| 46 | - 'restrictions' => $request->getSafe('restrictions', null, []), | |
| 47 | - ]; | |
| 34 | + $data = $this->sanitizeAgentPayload($request, true); | |
| 48 | 35 | |
| 49 | 36 | try { |
| 50 | 37 | return [ |
| 51 | 38 | 'message' => __('Support Staff has been added', 'fluent-support'), |
| @@ -50,21 +37,19 @@ | ||
| 50 | 37 | return [ |
| 51 | 38 | 'message' => __('Support Staff has been added', 'fluent-support'), |
| 52 | 39 | 'agent' => $agent->createAgent($data) |
| 53 | 40 | ]; |
| 54 | - | |
| 55 | 41 | } catch (\Exception $e) { |
| 56 | 42 | return $this->sendError([ |
| 57 | - 'message' => $e->getMessage() | |
| 43 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 58 | 44 | ]); |
| 59 | 45 | } |
| 60 | 46 | } |
| 61 | 47 | |
| 62 | 48 | /** |
| 63 | - * updateAgent method will update the information of an exiting agent | |
| 64 | 49 | * @param AgentCreateRequest $request |
| 65 | 50 | * @param Agent $agent |
| 66 | - * @param $agent_id | |
| 51 | + * @param int $agent_id | |
| 67 | 52 | * @return \WP_REST_Response | array |
| 68 | 53 | * @throws \FluentSupport\Framework\Validator\ValidationException |
| 69 | 54 | */ |
| 70 | 55 | public function updateAgent(AgentCreateRequest $request, Agent $agent, $agent_id) |
| @@ -69,44 +54,32 @@ | ||
| 69 | 54 | */ |
| 70 | 55 | public function updateAgent(AgentCreateRequest $request, Agent $agent, $agent_id) |
| 71 | 56 | { |
| 72 | 57 | $agent = $agent::findOrFail($agent_id); |
| 58 | + $data = $this->sanitizeAgentPayload($request); | |
| 73 | 59 | |
| 74 | - $data = [ | |
| 75 | - 'first_name' => $request->getSafe('first_name', 'sanitize_text_field'), | |
| 76 | - 'last_name' => $request->getSafe('last_name', 'sanitize_text_field'), | |
| 77 | - 'title' => $request->getSafe('title', 'sanitize_text_field'), | |
| 78 | - 'permissions' => $request->getSafe('permissions', null, []), | |
| 79 | - 'telegram_chat_id' => $request->getSafe('telegram_chat_id', 'sanitize_text_field'), | |
| 80 | - 'slack_user_id' => $request->getSafe('slack_user_id', 'sanitize_text_field'), | |
| 81 | - 'whatsapp_number' => $request->getSafe('whatsapp_number', 'sanitize_text_field'), | |
| 82 | - 'restrictions' => $request->getSafe('restrictions', null, []), | |
| 83 | - ]; | |
| 84 | - | |
| 85 | 60 | if (!$agent->user_id && ($user = get_user_by('email', $agent->email))) { |
| 86 | 61 | $agent->user_id = $user->ID; |
| 87 | 62 | } |
| 88 | 63 | |
| 89 | - if ($agent) { | |
| 90 | - try { | |
| 91 | - return [ | |
| 92 | - 'message' => __('Support Staff has been updated', 'fluent-support'), | |
| 93 | - 'agent' => $agent->updateAgent($data, $agent) | |
| 94 | - ]; | |
| 95 | - } catch (\Exception $e) { | |
| 96 | - return $this->sendError([ | |
| 97 | - 'message' => $e->getMessage() | |
| 98 | - ]); | |
| 99 | - } | |
| 64 | + try { | |
| 65 | + return [ | |
| 66 | + 'message' => __('Support Staff has been updated', 'fluent-support'), | |
| 67 | + 'agent' => $agent->updateAgent($data, $agent) | |
| 68 | + ]; | |
| 69 | + } catch (\Exception $e) { | |
| 70 | + return $this->sendError([ | |
| 71 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 72 | + ]); | |
| 100 | 73 | } |
| 101 | - | |
| 102 | 74 | } |
| 103 | 75 | |
| 104 | 76 | /** |
| 105 | - * deleteAgent will delete an exiting agent and add an alternative agent as replacement | |
| 77 | + * The agent's tickets and data move to fallback_agent_id. | |
| 78 | + * | |
| 106 | 79 | * @param Request $request |
| 107 | 80 | * @param Agent $agent |
| 108 | - * @param $agent_id | |
| 81 | + * @param int $agent_id | |
| 109 | 82 | * @return \WP_REST_Response | array |
| 110 | 83 | * @throws \FluentSupport\Framework\Validator\ValidationException |
| 111 | 84 | */ |
| 112 | 85 | public function deleteAgent(Request $request, Agent $agent, $agent_id) |
| @@ -116,15 +89,13 @@ | ||
| 116 | 89 | |
| 117 | 90 | return [ |
| 118 | 91 | 'message' => __('Support Staff has been deleted', 'fluent-support') |
| 119 | 92 | ]; |
| 120 | - | |
| 121 | 93 | } catch (\Exception $e) { |
| 122 | 94 | return $this->sendError([ |
| 123 | - 'message' => $e->getMessage() | |
| 95 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 124 | 96 | ]); |
| 125 | 97 | } |
| 126 | - | |
| 127 | 98 | } |
| 128 | 99 | |
| 129 | 100 | /** |
| 130 | 101 | * @param Request $request |
| @@ -131,25 +102,25 @@ | ||
| 131 | 102 | * @return \WP_REST_Response | array |
| 132 | 103 | */ |
| 133 | 104 | public function myStats(Request $request) |
| 134 | 105 | { |
| 106 | + $agent = Helper::getAgentByUserId(); | |
| 135 | 107 | |
| 136 | - $agent = Helper::getAgentByUserId();//Get logged in agent information | |
| 137 | - | |
| 138 | 108 | try { |
| 139 | - $stats = StatModule::getAgentStat($agent->id); //Get ticket statistics | |
| 109 | + $stats = StatModule::getAgentStat($agent->id); | |
| 110 | + $with = $request->get('with', []); | |
| 111 | + $with = is_array($with) ? map_deep($with, 'sanitize_text_field') : []; | |
| 140 | 112 | |
| 141 | - $with = $request->getSafe('with'); | |
| 142 | - | |
| 143 | 113 | $response = (new Agent())->getAgentStat($stats, $with, $agent->id); |
| 144 | 114 | |
| 145 | 115 | if (defined('FLUENTSUPPORTPRO')) { |
| 146 | 116 | $response['dashboard_notice'] = apply_filters('fluent_support/dashboard_notice', '', $agent); |
| 147 | 117 | } |
| 118 | + | |
| 148 | 119 | return $response; |
| 149 | 120 | } catch (\Exception $e) { |
| 150 | 121 | return $this->sendError([ |
| 151 | - 'message' => $e->getMessage() | |
| 122 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 152 | 123 | ]); |
| 153 | 124 | } |
| 154 | 125 | } |
| 155 | 126 | |
| @@ -157,13 +128,14 @@ | ||
| 157 | 128 | { |
| 158 | 129 | try { |
| 159 | 130 | if (PermissionManager::currentUserCan('fst_agent_today_performance')) { |
| 160 | 131 | $agentTodayStats = StatModule::getAgentTodayStats(); |
| 132 | + | |
| 161 | 133 | return ['agent_today_stats' => $agentTodayStats]; |
| 162 | 134 | } |
| 163 | 135 | } catch (\Exception $e) { |
| 164 | 136 | return $this->sendError([ |
| 165 | - 'message' => $e->getMessage() | |
| 137 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 166 | 138 | ]); |
| 167 | 139 | } |
| 168 | 140 | |
| 169 | 141 | return []; |
| @@ -169,10 +141,8 @@ | ||
| 169 | 141 | return []; |
| 170 | 142 | } |
| 171 | 143 | |
| 172 | 144 | /** |
| 173 | - * addOrUpdateProfileImage method will upload profile picture for a given agent id | |
| 174 | - * For a successful upload it's required to send file object, agent id and the user type(agent) | |
| 175 | 145 | * @param Request $request |
| 176 | 146 | * @param AvatarUploder $avatarUploder |
| 177 | 147 | * @return \WP_REST_Response | array |
| 178 | 148 | */ |
| @@ -178,41 +148,43 @@ | ||
| 178 | 148 | */ |
| 179 | 149 | public function addOrUpdateProfileImage(Request $request, AvatarUploder $avatarUploder) |
| 180 | 150 | { |
| 181 | 151 | try { |
| 182 | - return $avatarUploder->addOrUpdateProfileImage( $request->files(), $request->getSafe('agent_id', 'intval'), 'agent'); | |
| 152 | + return $avatarUploder->addOrUpdateProfileImage($request->files(), $request->getSafe('agent_id', 'intval'), 'agent'); | |
| 183 | 153 | } catch (\Exception $e) { |
| 184 | 154 | return $this->sendError([ |
| 185 | - 'message' => $e->getMessage() | |
| 155 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 186 | 156 | ]); |
| 187 | 157 | } |
| 188 | 158 | } |
| 189 | 159 | |
| 190 | 160 | /** |
| 191 | - * resetAvatar method will restore a Support Staff avatar | |
| 192 | - * For a successful upload it's required to send file object, Support Staff id and the user type(Support Staff) | |
| 193 | - * @param Agent $agent | |
| 194 | - * @param $agent_id | |
| 161 | + * No Agent type-hint: route-model binding runs before the policy, which would | |
| 162 | + * let unauthenticated callers probe agent IDs. Resolve after auth. | |
| 163 | + * | |
| 164 | + * @param int|string $agent | |
| 195 | 165 | * @return array |
| 196 | 166 | */ |
| 197 | - public function resetAvatar(Agent $agent, $agent_id){ | |
| 167 | + public function resetAvatar($agent) | |
| 168 | + { | |
| 198 | 169 | try { |
| 199 | - $agent->restoreAvatar($agent, $agent_id); | |
| 170 | + $agent = Agent::findOrFail((int) $agent); | |
| 171 | + $agent->restoreAvatar(); | |
| 200 | 172 | |
| 201 | 173 | return [ |
| 202 | - 'message' => __('Support Staff avatar reset to gravatar default', 'fluent-support') | |
| 174 | + 'message' => __('Support Staff avatar reset to gravatar default', 'fluent-support') | |
| 203 | 175 | ]; |
| 204 | 176 | } catch (\Exception $e) { |
| 205 | - return [ | |
| 206 | - 'message' => $e->getMessage() | |
| 207 | - ]; | |
| 177 | + return $this->sendError([ | |
| 178 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 179 | + ]); | |
| 208 | 180 | } |
| 209 | 181 | } |
| 210 | 182 | |
| 211 | 183 | public function getAgentInsights(Request $request, Agent $agent) |
| 212 | - { | |
| 184 | + { | |
| 213 | 185 | return [ |
| 214 | - 'agents' => $agent->agentInsights($request->getSafe('search','sanitize_text_field')), | |
| 186 | + 'agents' => $agent->agentInsights($request->getSafe('search', 'sanitize_text_field')), | |
| 215 | 187 | ]; |
| 216 | 188 | } |
| 217 | 189 | |
| 218 | 190 | public function ping(Request $request) |
| @@ -217,8 +189,69 @@ | ||
| 217 | 189 | |
| 218 | 190 | public function ping(Request $request) |
| 219 | 191 | { |
| 220 | 192 | return [ |
| 221 | - 'ping' => 'pong' | |
| 193 | + 'ping' => 'pong', | |
| 194 | + ]; | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * @param AgentCreateRequest $request | |
| 199 | + * @param bool $includeEmail | |
| 200 | + * @return array | |
| 201 | + */ | |
| 202 | + private function sanitizeAgentPayload(AgentCreateRequest $request, $includeEmail = false) | |
| 203 | + { | |
| 204 | + $data = [ | |
| 205 | + 'first_name' => $request->getSafe('first_name', 'sanitize_text_field'), | |
| 206 | + 'last_name' => $request->getSafe('last_name', 'sanitize_text_field'), | |
| 207 | + 'title' => $request->getSafe('title', 'sanitize_text_field'), | |
| 208 | + 'permissions' => is_array($request->get('permissions')) ? array_map('sanitize_key', $request->get('permissions')) : [], | |
| 209 | + 'telegram_chat_id' => $request->getSafe('telegram_chat_id', 'sanitize_text_field'), | |
| 210 | + 'slack_user_id' => $request->getSafe('slack_user_id', 'sanitize_text_field'), | |
| 211 | + 'whatsapp_number' => $request->getSafe('whatsapp_number', 'sanitize_text_field'), | |
| 212 | + 'restrictions' => $this->sanitizeRestrictions($request->get('restrictions')), | |
| 213 | + ]; | |
| 214 | + | |
| 215 | + if ($request->has('agent_signature')) { | |
| 216 | + // Already unslashed by the request. Don't unslash again or literal backslashes are lost. | |
| 217 | + $data['agent_signature'] = wp_kses_post($request->get('agent_signature', '')); | |
| 218 | + } | |
| 219 | + | |
| 220 | + if ($request->has('agent_signature_enabled')) { | |
| 221 | + $data['agent_signature_enabled'] = $request->get('agent_signature_enabled') === 'yes' ? 'yes' : 'no'; | |
| 222 | + } | |
| 223 | + | |
| 224 | + if ($includeEmail) { | |
| 225 | + $data['email'] = $request->getSafe('email', 'sanitize_email'); | |
| 226 | + } | |
| 227 | + | |
| 228 | + return $data; | |
| 229 | + } | |
| 230 | + | |
| 231 | + /** | |
| 232 | + * @param mixed $restrictions | |
| 233 | + * @return array | |
| 234 | + */ | |
| 235 | + private function sanitizeRestrictions($restrictions) | |
| 236 | + { | |
| 237 | + if (!is_array($restrictions)) { | |
| 238 | + return [ | |
| 239 | + 'restrictedBusinessBoxes' => [], | |
| 240 | + 'businessBoxRestrictions' => false, | |
| 241 | + ]; | |
| 242 | + } | |
| 243 | + | |
| 244 | + $restrictedBusinessBoxes = isset($restrictions['restrictedBusinessBoxes']) && is_array($restrictions['restrictedBusinessBoxes']) | |
| 245 | + ? array_map('absint', $restrictions['restrictedBusinessBoxes']) | |
| 246 | + : []; | |
| 247 | + | |
| 248 | + $businessBoxRestrictions = isset($restrictions['businessBoxRestrictions']) | |
| 249 | + ? rest_sanitize_boolean($restrictions['businessBoxRestrictions']) | |
| 250 | + : false; | |
| 251 | + | |
| 252 | + return [ | |
| 253 | + 'restrictedBusinessBoxes' => $restrictedBusinessBoxes, | |
| 254 | + 'businessBoxRestrictions' => $businessBoxRestrictions, | |
| 222 | 255 | ]; |
| 223 | 256 | } |
| 224 | 257 | } |