Commit c4e1ececa4e30d2193be03a6c1792f938b05e3c0
1 parent
f8fab208
delete action for users
Showing
6 changed files
with
69 additions
and
26 deletions
Show diff stats
app/library/App/Controllers/UserController.php
| @@ -17,12 +17,12 @@ class UserController extends CrudResourceController | @@ -17,12 +17,12 @@ class UserController extends CrudResourceController | ||
| 17 | ]; | 17 | ]; |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | - public function me() | 20 | + public function meAction() |
| 21 | { | 21 | { |
| 22 | return $this->createResourceResponse($this->userService->getDetails()); | 22 | return $this->createResourceResponse($this->userService->getDetails()); |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | - public function authenticate() | 25 | + public function authenticateAction() |
| 26 | { | 26 | { |
| 27 | $username = $this->request->getUsername(); | 27 | $username = $this->request->getUsername(); |
| 28 | $password = $this->request->getPassword(); | 28 | $password = $this->request->getPassword(); |
| @@ -44,7 +44,7 @@ class UserController extends CrudResourceController | @@ -44,7 +44,7 @@ class UserController extends CrudResourceController | ||
| 44 | return $this->createArrayResponse($response, 'data'); | 44 | return $this->createArrayResponse($response, 'data'); |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | - public function register() { | 47 | + public function registerAction() { |
| 48 | 48 | ||
| 49 | $this->beforeHandle(); | 49 | $this->beforeHandle(); |
| 50 | $this->beforeHandleWrite(); | 50 | $this->beforeHandleWrite(); |
app/library/App/Resources/UserResource.php
| @@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Resources; | 3 | namespace App\Resources; |
| 4 | 4 | ||
| 5 | +use PhalconApi\Constants\HttpMethods; | ||
| 5 | use PhalconRest\Api\ApiResource; | 6 | use PhalconRest\Api\ApiResource; |
| 6 | use PhalconRest\Api\ApiEndpoint; | 7 | use PhalconRest\Api\ApiEndpoint; |
| 7 | use App\Model\User; | 8 | use App\Model\User; |
| @@ -18,44 +19,74 @@ class UserResource extends ApiResource { | @@ -18,44 +19,74 @@ class UserResource extends ApiResource { | ||
| 18 | ->model(User::class) | 19 | ->model(User::class) |
| 19 | ->expectsJsonData() | 20 | ->expectsJsonData() |
| 20 | ->transformer(UserTransformer::class) | 21 | ->transformer(UserTransformer::class) |
| 21 | - ->handler(UserController::class) | ||
| 22 | ->itemKey('user') | 22 | ->itemKey('user') |
| 23 | ->collectionKey('users') | 23 | ->collectionKey('users') |
| 24 | ->deny(AclRoles::UNAUTHORIZED, AclRoles::USER) | 24 | ->deny(AclRoles::UNAUTHORIZED, AclRoles::USER) |
| 25 | + ->handler(UserController::class); | ||
| 25 | 26 | ||
| 27 | + $this | ||
| 26 | ->endpoint(ApiEndpoint::all() | 28 | ->endpoint(ApiEndpoint::all() |
| 27 | ->allow(AclRoles::USER) | 29 | ->allow(AclRoles::USER) |
| 28 | - ->description('Returns all registered users') | ||
| 29 | - ) | ||
| 30 | - ->endpoint(ApiEndpoint::get('/me', 'me') | 30 | + ->name('all') |
| 31 | + ->description('Возвращает всех зарегистрированных пользователей') | ||
| 32 | + ); | ||
| 33 | + $this | ||
| 34 | + ->endpoint(ApiEndpoint::factory('/me', HttpMethods::GET, 'meAction') | ||
| 35 | + ->name('me') | ||
| 36 | + ->description('Возвращает текущего залогиненного пользователя') | ||
| 31 | ->allow(AclRoles::USER) | 37 | ->allow(AclRoles::USER) |
| 32 | - ->description('Returns the currently logged in user') | ||
| 33 | - ) | ||
| 34 | - ->endpoint(ApiEndpoint::post('/authenticate', 'authenticate') | 38 | + ); |
| 39 | + | ||
| 40 | + $this | ||
| 41 | + ->endpoint(ApiEndpoint::factory('/authenticate', HttpMethods::POST, 'authenticateAction') | ||
| 42 | + ->name('authenticate') | ||
| 43 | + ->description('Авторизация пользователя через BasicAuth и возвращает токен доступа') | ||
| 35 | ->allow(AclRoles::UNAUTHORIZED) | 44 | ->allow(AclRoles::UNAUTHORIZED) |
| 36 | ->deny(AclRoles::AUTHORIZED) | 45 | ->deny(AclRoles::AUTHORIZED) |
| 37 | - ->description('Authenticates user credentials provided in the authorization header and returns an access token') | ||
| 38 | ->exampleResponse([ | 46 | ->exampleResponse([ |
| 39 | - 'token' => 'co126bbm40wqp41i3bo7pj1gfsvt9lp6', | ||
| 40 | - 'expires' => 1451139067 | 47 | + 'data' => [ |
| 48 | + 'token' => 'co126bbm40wqp41i3bo7pj1gfsvt9lp6', | ||
| 49 | + 'expires' => 1451139067, | ||
| 50 | + "user" => | ||
| 51 | + [ | ||
| 52 | + "id" => 1, | ||
| 53 | + "username" => "demo", | ||
| 54 | + "email" => "test@example.com", | ||
| 55 | + "role" => "User", | ||
| 56 | + "createdAt" => "2015-12-28 16:20:58", | ||
| 57 | + "updatedAt" => null | ||
| 58 | + ] | ||
| 59 | + ] | ||
| 60 | + | ||
| 41 | ]) | 61 | ]) |
| 42 | - ) | ||
| 43 | - ->endpoint(ApiEndpoint::post('/', 'register') | 62 | + ); |
| 63 | + $this | ||
| 64 | + ->endpoint(ApiEndpoint::factory('/', HttpMethods::POST, 'registerAction') | ||
| 65 | + ->name('register') | ||
| 66 | + ->description('Регистрация нового пользователя') | ||
| 44 | ->allow(AclRoles::UNAUTHORIZED) | 67 | ->allow(AclRoles::UNAUTHORIZED) |
| 45 | ->deny(AclRoles::AUTHORIZED) | 68 | ->deny(AclRoles::AUTHORIZED) |
| 46 | - ->description('Register new user') | ||
| 47 | - ->expectsJsonData() | ||
| 48 | ->exampleResponse([ | 69 | ->exampleResponse([ |
| 49 | - "result" => "OK", | ||
| 50 | - "user" => [ | ||
| 51 | - "id" => "95", | ||
| 52 | - "username" => "MyLogin", | ||
| 53 | - "email" => "myGmail@gmail.com", | ||
| 54 | - "role" => "User", | ||
| 55 | - "createdAt" => "2017-02-16 17:57:52", | ||
| 56 | - "updatedAt" => "2017-02-16 17:57:52" | ||
| 57 | - ] | 70 | + "result" => "OK", |
| 71 | + "user" => [ | ||
| 72 | + "id" => "95", | ||
| 73 | + "username" => "MyLogin", | ||
| 74 | + "email" => "myGmail@gmail.com", | ||
| 75 | + "role" => "User", | ||
| 76 | + "createdAt" => "2017-02-16 17:57:52", | ||
| 77 | + "updatedAt" => "2017-02-16 17:57:52" | ||
| 78 | + ] | ||
| 58 | ]) | 79 | ]) |
| 59 | ); | 80 | ); |
| 81 | + $this | ||
| 82 | + ->endpoint(ApiEndpoint::remove() | ||
| 83 | + ->name('remove') | ||
| 84 | + ->description('Удаление пользователя') | ||
| 85 | + ->allow(AclRoles::USER) | ||
| 86 | + ->exampleResponse([ | ||
| 87 | + "result" => "OK" | ||
| 88 | + ]) | ||
| 89 | + ) | ||
| 90 | + ; | ||
| 60 | } | 91 | } |
| 61 | } | 92 | } |
| 62 | \ No newline at end of file | 93 | \ No newline at end of file |