Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit 9169cb5

Browse files
committed
- Get logged in user profile #7
- Update logged in user profile #9
1 parent f752208 commit 9169cb5

File tree

8 files changed

+693
-15
lines changed

8 files changed

+693
-15
lines changed

app/Entities/User.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ class User extends Authenticatable
3939
];
4040

4141
/**
42-
* Set the user's password
43-
*
44-
* @param string $value
45-
* @return void
42+
* @param array $attributes
43+
* @return \Illuminate\Database\Eloquent\Model
4644
*/
47-
public function setPasswordAttribute($value)
45+
public static function create(array $attributes = [])
4846
{
49-
$this->attributes['password'] = bcrypt($value);
47+
48+
if(array_key_exists('password', $attributes)) {
49+
$attributes['password'] = bcrypt($attributes['password']);
50+
}
51+
52+
$model = static::query()->create($attributes);
53+
54+
return $model;
5055
}
5156
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Users;
4+
5+
use Illuminate\Http\Request;
6+
use Dingo\Api\Routing\Helpers;
7+
use App\Http\Controllers\Controller;
8+
use Illuminate\Support\Facades\Auth;
9+
use App\Transformers\Users\UserTransformer;
10+
11+
/**
12+
* Class ProfileController
13+
* @package App\Http\Controllers\Api\Users
14+
*/
15+
class ProfileController extends Controller
16+
{
17+
18+
use Helpers;
19+
20+
/**
21+
* @return \Dingo\Api\Http\Response
22+
*/
23+
public function index()
24+
{
25+
return $this->response->item(Auth::user(), new UserTransformer());
26+
}
27+
28+
/**
29+
* @param Request $request
30+
* @return \Dingo\Api\Http\Response
31+
*/
32+
public function update(Request $request)
33+
{
34+
$user = Auth::user();
35+
$rules = [
36+
'name' => 'required',
37+
'email' => 'required|email|unique:users,email,'.$user->id,
38+
];
39+
if($request->method() == "PATCH") {
40+
$rules = [
41+
'name' => 'sometimes|required',
42+
'email' => 'sometimes|required|email|unique:users,email,'.$user->id,
43+
];
44+
}
45+
$this->validate($request, $rules);
46+
// Except password as we don't want to let the users change a password from this endpoint
47+
$user->update($request->except('_token', 'password'));
48+
return $this->response->item($user->fresh(), new UserTransformer());
49+
}
50+
51+
}

docs/api/blueprint/dataStructures/users.apib

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
## User Update (object)
2323
- name: `Jhon Doe` (string, optional)
24-
- email: `[email protected]` (string, optional)
2524

2625
## User Update Full (object)
2726
- name: `Jhon Doe` (string, required)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Users profile [/api/me]
2+
3+
### Get logged in user profile [GET]
4+
5+
+ Request (application/vnd.api.v1+json)
6+
7+
+ Headers
8+
9+
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjBlOGVhOGY0YWY2Yjg3NzU0YjZjZDBjNjdkNzBmMjdhYTZkMmRjNjU3YzIwYTU0MjQ3NWRlZGU4Y2VkNTc1N2IwZDcwMWFmNTAyMGU4MGE4In0.eyJhdWQiOiIxIiwianRpIjoiMGU4ZWE4ZjRhZjZiODc3NTRiNmNkMGM2N2Q3MGYyN2FhNmQyZGM2NTdjMjBhNTQyNDc1ZGVkZThjZWQ1NzU3YjBkNzAxYWY1MDIwZTgwYTgiLCJpYXQiOjE0NzYyOTk1NTQsIm5iZiI6MTQ3NjI5OTU1NCwiZXhwIjoxNDc3NTk1NTU0LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.gTOotTVp2eF_HvuW48ngGqdly4bocNMlvY0al0YmYPJ_tjBQYegE4AjtCH2bWUf9aUz8tQqhMr2UFae3JV6VDQC9VzoGK8gU_nulM0BkqaCCxadqrw_slcd4he9hT0FE3WshvdZKcmgaVovgrI2-sAnX3n749BiWtWLEpw6x-TdEHEVcW6uRfRWyrwKhPBfOk-hXpgbecgT8LYdBW531P1ryjSJzzi5zBEe0Ecbp9Lo-fV69AW8ZBnG1DBjzxW-hvE0rRLXXnI4-f9rbQfK6QUzfG3Dg2INexcKxu9sQ3Vn5wHhQyu4_nTNQh8rsgphdAqFnEo-FQZrRWejkKbOi_BcpQybaXNn7Qln-96QF6PAzT-2E08VmL4XeVNcSvmM1sPVwirnSXo4UIFSsHvvIdVfEYEXD8XDonlnVO74RgBDMXWs6xo7dmDNaqQxKt9J_s_xtmyM3w62C4QucKw7MY0zOqviEyXySbrYbgazO_Pl1--GXtksE8tVMW8OW6Y8fw0JE0GEd5hZVadR277A5164QAJhiGXI_mKNTtUZVNaj0JpKEQpu4tod_BJR_DxzvcpKHwc1YrfrPOul6mL4kZeafkehPe79jxrCXKgKEuGlgSFcyrXUNCtME9LxsMID6QpJ-tfx4i0jqjixd_smqpjPPYqhPuqhox3uMLcDgM6s
10+
11+
+ Response 200 (application/json)
12+
13+
+ Attributes
14+
+ data (User Object)
15+
16+
+ Response 401 (application/json)
17+
18+
+ Attributes (Error 401)
19+
20+
+ Response 403 (application/json)
21+
22+
+ Attributes (Error 403)
23+
24+
+ Response 404 (application/json)
25+
26+
+ Attributes (Error 404)
27+
28+
### Full update logged in user profile [PUT]
29+
30+
+ Request (application/vnd.api.v1+json)
31+
32+
+ Headers
33+
34+
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjBlOGVhOGY0YWY2Yjg3NzU0YjZjZDBjNjdkNzBmMjdhYTZkMmRjNjU3YzIwYTU0MjQ3NWRlZGU4Y2VkNTc1N2IwZDcwMWFmNTAyMGU4MGE4In0.eyJhdWQiOiIxIiwianRpIjoiMGU4ZWE4ZjRhZjZiODc3NTRiNmNkMGM2N2Q3MGYyN2FhNmQyZGM2NTdjMjBhNTQyNDc1ZGVkZThjZWQ1NzU3YjBkNzAxYWY1MDIwZTgwYTgiLCJpYXQiOjE0NzYyOTk1NTQsIm5iZiI6MTQ3NjI5OTU1NCwiZXhwIjoxNDc3NTk1NTU0LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.gTOotTVp2eF_HvuW48ngGqdly4bocNMlvY0al0YmYPJ_tjBQYegE4AjtCH2bWUf9aUz8tQqhMr2UFae3JV6VDQC9VzoGK8gU_nulM0BkqaCCxadqrw_slcd4he9hT0FE3WshvdZKcmgaVovgrI2-sAnX3n749BiWtWLEpw6x-TdEHEVcW6uRfRWyrwKhPBfOk-hXpgbecgT8LYdBW531P1ryjSJzzi5zBEe0Ecbp9Lo-fV69AW8ZBnG1DBjzxW-hvE0rRLXXnI4-f9rbQfK6QUzfG3Dg2INexcKxu9sQ3Vn5wHhQyu4_nTNQh8rsgphdAqFnEo-FQZrRWejkKbOi_BcpQybaXNn7Qln-96QF6PAzT-2E08VmL4XeVNcSvmM1sPVwirnSXo4UIFSsHvvIdVfEYEXD8XDonlnVO74RgBDMXWs6xo7dmDNaqQxKt9J_s_xtmyM3w62C4QucKw7MY0zOqviEyXySbrYbgazO_Pl1--GXtksE8tVMW8OW6Y8fw0JE0GEd5hZVadR277A5164QAJhiGXI_mKNTtUZVNaj0JpKEQpu4tod_BJR_DxzvcpKHwc1YrfrPOul6mL4kZeafkehPe79jxrCXKgKEuGlgSFcyrXUNCtME9LxsMID6QpJ-tfx4i0jqjixd_smqpjPPYqhPuqhox3uMLcDgM6s
35+
36+
+ Attributes (User Update Full)
37+
38+
+ Response 200 (application/json)
39+
40+
+ Attributes
41+
+ data (User Object)
42+
43+
+ Response 401 (application/json)
44+
45+
+ Attributes (Error 401)
46+
47+
+ Response 403 (application/json)
48+
49+
+ Attributes (Error 403)
50+
51+
+ Response 404 (application/json)
52+
53+
+ Attributes (Error 404)
54+
55+
### Partial update logged in user profile [PATCH]
56+
57+
+ Request (application/vnd.api.v1+json)
58+
59+
+ Headers
60+
61+
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjBlOGVhOGY0YWY2Yjg3NzU0YjZjZDBjNjdkNzBmMjdhYTZkMmRjNjU3YzIwYTU0MjQ3NWRlZGU4Y2VkNTc1N2IwZDcwMWFmNTAyMGU4MGE4In0.eyJhdWQiOiIxIiwianRpIjoiMGU4ZWE4ZjRhZjZiODc3NTRiNmNkMGM2N2Q3MGYyN2FhNmQyZGM2NTdjMjBhNTQyNDc1ZGVkZThjZWQ1NzU3YjBkNzAxYWY1MDIwZTgwYTgiLCJpYXQiOjE0NzYyOTk1NTQsIm5iZiI6MTQ3NjI5OTU1NCwiZXhwIjoxNDc3NTk1NTU0LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.gTOotTVp2eF_HvuW48ngGqdly4bocNMlvY0al0YmYPJ_tjBQYegE4AjtCH2bWUf9aUz8tQqhMr2UFae3JV6VDQC9VzoGK8gU_nulM0BkqaCCxadqrw_slcd4he9hT0FE3WshvdZKcmgaVovgrI2-sAnX3n749BiWtWLEpw6x-TdEHEVcW6uRfRWyrwKhPBfOk-hXpgbecgT8LYdBW531P1ryjSJzzi5zBEe0Ecbp9Lo-fV69AW8ZBnG1DBjzxW-hvE0rRLXXnI4-f9rbQfK6QUzfG3Dg2INexcKxu9sQ3Vn5wHhQyu4_nTNQh8rsgphdAqFnEo-FQZrRWejkKbOi_BcpQybaXNn7Qln-96QF6PAzT-2E08VmL4XeVNcSvmM1sPVwirnSXo4UIFSsHvvIdVfEYEXD8XDonlnVO74RgBDMXWs6xo7dmDNaqQxKt9J_s_xtmyM3w62C4QucKw7MY0zOqviEyXySbrYbgazO_Pl1--GXtksE8tVMW8OW6Y8fw0JE0GEd5hZVadR277A5164QAJhiGXI_mKNTtUZVNaj0JpKEQpu4tod_BJR_DxzvcpKHwc1YrfrPOul6mL4kZeafkehPe79jxrCXKgKEuGlgSFcyrXUNCtME9LxsMID6QpJ-tfx4i0jqjixd_smqpjPPYqhPuqhox3uMLcDgM6s
62+
63+
+ Attributes (User Update)
64+
65+
+ Response 200 (application/json)
66+
67+
+ Attributes
68+
+ data (User Object)
69+
70+
+ Response 401 (application/json)
71+
72+
+ Attributes (Error 401)
73+
74+
+ Response 403 (application/json)
75+
76+
+ Attributes (Error 403)
77+
78+
+ Response 404 (application/json)
79+
80+
+ Attributes (Error 404)

docs/api/blueprint/routes/users.apib

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,5 @@ This endpoint will allow you to delete a user from the system
203203
+ Attributes (Error 404)
204204

205205
<!-- include(roles.apib) -->
206-
<!-- include(permissions.apib) -->
206+
<!-- include(permissions.apib) -->
207+
<!-- include(profile.apib) -->

resources/views/apidocs.blade.php

Lines changed: 448 additions & 7 deletions
Large diffs are not rendered by default.

routes/api.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
$api->get('permissions', 'Api\Users\PermissionsController@index');
3232

33+
$api->group(['prefix' => 'me'], function($api) {
34+
$api->get('/', 'Api\Users\ProfileController@index');
35+
$api->put('/', 'Api\Users\ProfileController@update');
36+
$api->patch('/', 'Api\Users\ProfileController@update');
37+
});
38+
3339
});
3440

3541
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Tests\Feature\Users;
4+
5+
use Tests\TestCase;
6+
use App\Entities\User;
7+
use Laravel\Passport\Passport;
8+
use Illuminate\Foundation\Testing\DatabaseMigrations;
9+
10+
class ProfileEndpointsTest extends TestCase
11+
{
12+
13+
use DatabaseMigrations;
14+
15+
function setUp()
16+
{
17+
parent::setUp();
18+
$this->installApp();
19+
}
20+
21+
function test_it_gets_user_profile()
22+
{
23+
Passport::actingAs(User::first());
24+
$response = $this->json('GET', 'api/me');
25+
$response->assertStatus(200);
26+
$response->assertJson([
27+
'data' => [
28+
'name' => 'Jose Fonseca',
29+
'email' => '[email protected]',
30+
'roles' => []
31+
]
32+
]);
33+
}
34+
35+
function test_it_can_update_logged_user_profile_all_entity()
36+
{
37+
Passport::actingAs(User::first());
38+
$response = $this->json('PUT', '/api/me', [
39+
'name' => 'Jose Fonseca Edited',
40+
'email' => '[email protected]'
41+
]);
42+
$response->assertStatus(200);
43+
$this->assertDatabaseHas('users', [
44+
'name' => 'Jose Fonseca Edited',
45+
'email' => '[email protected]',
46+
]);
47+
$response->assertJson([
48+
'data' => [
49+
'name' => 'Jose Fonseca Edited',
50+
'email' => '[email protected]',
51+
'roles' => []
52+
]
53+
]);
54+
}
55+
56+
function test_it_can_update_profile_partial_entity()
57+
{
58+
Passport::actingAs(User::first());
59+
$response = $this->json('PATCH', '/api/me', [
60+
'name' => 'Jose Fonseca Edited'
61+
]);
62+
$response->assertStatus(200);
63+
$this->assertDatabaseHas('users', [
64+
'name' => 'Jose Fonseca Edited',
65+
'email' => '[email protected]',
66+
]);
67+
$response->assertJson([
68+
'data' => [
69+
'name' => 'Jose Fonseca Edited',
70+
'email' => '[email protected]',
71+
'roles' => []
72+
]
73+
]);
74+
}
75+
76+
function test_it_validates_input_for_update_profile()
77+
{
78+
Passport::actingAs(User::first());
79+
$response = $this->json('PATCH', '/api/me', [
80+
'name' => ''
81+
]);
82+
$response->assertStatus(422);
83+
}
84+
85+
function test_it_validates_input_for_email_on_update_profile()
86+
{
87+
Passport::actingAs(User::first());
88+
$user = factory(User::class)->create();
89+
$response = $this->json('PATCH', '/api/me', [
90+
'email' => $user->email
91+
]);
92+
$response->assertStatus(422);
93+
}
94+
95+
}

0 commit comments

Comments
 (0)