3e2c7382d9
* from 5.6.x to 5.7.x * from 5.7.x to 5.8.x * from 5.8.x to 6.x * from 6.x to 7.x * from 7.x to 8.x * updated to work with php7.4 and mysql 8.x Change-Id: I5fa8af21e13ce383d6648a660b1aac7e5ce55fa5 Signed-off-by: smarcet <smarcet@gmail.com>
270 lines
7.7 KiB
PHP
270 lines
7.7 KiB
PHP
<?php namespace App\Http\Controllers\Api;
|
|
/**
|
|
* Copyright 2015 OpenStack Foundation
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
**/
|
|
|
|
use App\Http\Controllers\APICRUDController;
|
|
use App\Http\Controllers\UserValidationRulesFactory;
|
|
use App\Http\Utils\HTMLCleaner;
|
|
use App\ModelSerializers\SerializerRegistry;
|
|
use Auth\Repositories\IUserRepository;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use models\exceptions\ValidationException;
|
|
use OAuth2\Services\ITokenService;
|
|
use OpenId\Services\IUserService;
|
|
use models\exceptions\EntityNotFoundException;
|
|
use Utils\Services\ILogService;
|
|
use Illuminate\Http\Request as LaravelRequest;
|
|
|
|
/**
|
|
* Class UserApiController
|
|
* @package App\Http\Controllers\Api
|
|
*/
|
|
final class UserApiController extends APICRUDController
|
|
{
|
|
|
|
/**
|
|
* @var ITokenService
|
|
*/
|
|
private $token_service;
|
|
|
|
/**
|
|
* UserApiController constructor.
|
|
* @param IUserRepository $user_repository
|
|
* @param ILogService $log_service
|
|
* @param IUserService $user_service
|
|
* @param ITokenService $token_service
|
|
*/
|
|
public function __construct
|
|
(
|
|
IUserRepository $user_repository,
|
|
ILogService $log_service,
|
|
IUserService $user_service,
|
|
ITokenService $token_service
|
|
)
|
|
{
|
|
parent::__construct($user_repository, $user_service, $log_service);
|
|
$this->token_service = $token_service;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getFilterRules(): array
|
|
{
|
|
return [
|
|
'first_name' => ['=@', '=='],
|
|
'last_name' => ['=@', '=='],
|
|
'full_name' => ['=@', '=='],
|
|
'email' => ['=@', '=='],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getFilterValidatorRules(): array
|
|
{
|
|
return [
|
|
'first_name' => 'nullable|string',
|
|
'last_name' => 'nullable|string',
|
|
'full_name' => 'nullable|string',
|
|
'email' => 'nullable|string',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getOrderRules():array{
|
|
return [
|
|
'first_name',
|
|
'last_name',
|
|
'email',
|
|
'identifier',
|
|
'last_login_date',
|
|
'spam_type'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public function unlock($id)
|
|
{
|
|
try {
|
|
$entity = $this->service->unlockUser($id);
|
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity)->serialize());
|
|
} catch (ValidationException $ex1) {
|
|
Log::warning($ex1);
|
|
return $this->error412(array($ex1->getMessage()));
|
|
} catch (EntityNotFoundException $ex2) {
|
|
Log::warning($ex2);
|
|
return $this->error404(array('message' => $ex2->getMessage()));
|
|
} catch (Exception $ex) {
|
|
Log::error($ex);
|
|
return $this->error500($ex);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public function lock($id)
|
|
{
|
|
try {
|
|
$entity = $this->service->lockUser($id);
|
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($entity)->serialize());
|
|
} catch (ValidationException $ex1) {
|
|
Log::warning($ex1);
|
|
return $this->error412(array($ex1->getMessage()));
|
|
} catch (EntityNotFoundException $ex2) {
|
|
Log::warning($ex2);
|
|
return $this->error404(array('message' => $ex2->getMessage()));
|
|
} catch (Exception $ex) {
|
|
Log::error($ex);
|
|
return $this->error500($ex);
|
|
}
|
|
}
|
|
|
|
protected function getAllSerializerType(): string
|
|
{
|
|
return SerializerRegistry::SerializerType_Private;
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @param $value
|
|
* @return mixed
|
|
*/
|
|
public function revokeMyToken($value)
|
|
{
|
|
|
|
try {
|
|
$hint = Request::input('hint', 'none');
|
|
|
|
switch ($hint) {
|
|
case 'access-token':
|
|
{
|
|
$this->token_service->revokeAccessToken($value, true);
|
|
}
|
|
break;
|
|
case 'refresh-token':
|
|
$this->token_service->revokeRefreshToken($value, true);
|
|
break;
|
|
default:
|
|
throw new Exception(sprintf("hint %s not allowed", $hint));
|
|
break;
|
|
}
|
|
return $this->deleted();
|
|
} catch (ValidationException $ex1) {
|
|
Log::warning($ex1);
|
|
return $this->error412(array($ex1->getMessage()));
|
|
} catch (EntityNotFoundException $ex2) {
|
|
Log::warning($ex2);
|
|
return $this->error404(array('message' => $ex2->getMessage()));
|
|
} catch (Exception $ex) {
|
|
Log::error($ex);
|
|
return $this->error500($ex);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getUpdatePayloadValidationRules(): array
|
|
{
|
|
return UserValidationRulesFactory::build([], true, true);
|
|
}
|
|
|
|
protected function curateUpdatePayload(array $payload): array
|
|
{
|
|
if(in_array("bio", $payload)){
|
|
$payload["bio"] = strip_tags($payload["bio"]);
|
|
}
|
|
if(in_array("statement_of_interest", $payload)){
|
|
$payload["statement_of_interest"] = strip_tags($payload["statement_of_interest"]);
|
|
}
|
|
return $payload;
|
|
}
|
|
|
|
protected function curateCreatePayload(array $payload): array
|
|
{
|
|
if(in_array("bio", $payload)){
|
|
$payload["bio"] = strip_tags($payload["bio"]);
|
|
}
|
|
if(in_array("statement_of_interest", $payload)){
|
|
$payload["statement_of_interest"] = strip_tags($payload["statement_of_interest"]);
|
|
}
|
|
return $payload;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getCreatePayloadValidationRules(): array
|
|
{
|
|
return UserValidationRulesFactory::build([], false, true);
|
|
}
|
|
|
|
/**
|
|
* @param LaravelRequest $request
|
|
* @return \Illuminate\Http\JsonResponse|mixed
|
|
*/
|
|
public function updateMe(LaravelRequest $request)
|
|
{
|
|
if (!Auth::check())
|
|
return $this->error403();
|
|
|
|
$myId = Auth::user()->getId();
|
|
return $this->update($myId);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getUpdatePayload():array{
|
|
$payload = request()->all();
|
|
if(isset($payload['user'])){
|
|
$payload = json_decode($payload['user'],true);
|
|
if(is_null($payload)){
|
|
Log::warning(sprintf("UserApiController::getUpdatePayload can not decode %s ", $payload['user']));
|
|
return [];
|
|
}
|
|
}
|
|
return $payload;
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @param $payload
|
|
* @return \models\utils\IEntity
|
|
*/
|
|
protected function onUpdate($id, $payload){
|
|
$user = parent::onUpdate($id, $payload);
|
|
$file = request()->file('pic');
|
|
if (!is_null($file)) {
|
|
$user = $this->service->updateProfilePhoto($id, $file);
|
|
}
|
|
return $user;
|
|
}
|
|
|
|
protected function serializerType(): string
|
|
{
|
|
return SerializerRegistry::SerializerType_Private;
|
|
}
|
|
} |