forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileTrait.php
More file actions
61 lines (55 loc) · 2.1 KB
/
Copy pathProfileTrait.php
File metadata and controls
61 lines (55 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Controller\Traits;
use Cake\Controller\Component\AuthComponent;
use Cake\Core\Configure;
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
use Cake\Datasource\Exception\RecordNotFoundException;
/**
* Covers the profile action
*
* @property \Cake\Http\ServerRequest $request
*/
trait ProfileTrait
{
/**
* Profile action
* @param mixed $id Profile id object.
* @return mixed
*/
public function profile($id = null)
{
$loggedUserId = $this->Auth->user('id');
$isCurrentUser = false;
if (!Configure::read('Users.Profile.viewOthers') || empty($id)) {
$id = $loggedUserId;
}
try {
$appContain = (array)Configure::read('Auth.authenticate.' . AuthComponent::ALL . '.contain');
$socialContain = Configure::read('Users.Social.login') ? ['SocialAccounts']: [];
$user = $this->getUsersTable()->get($id, [
'contain' => array_merge((array)$appContain, (array)$socialContain)
]);
$this->set('avatarPlaceholder', Configure::read('Users.Avatar.placeholder'));
if ($user->id === $loggedUserId) {
$isCurrentUser = true;
}
} catch (RecordNotFoundException $ex) {
$this->Flash->error(__d('CakeDC/Users', 'User was not found'));
return $this->redirect($this->request->referer());
} catch (InvalidPrimaryKeyException $ex) {
$this->Flash->error(__d('CakeDC/Users', 'Not authorized, please login first'));
return $this->redirect($this->request->referer());
}
$this->set(compact('user', 'isCurrentUser'));
$this->set('_serialize', ['user', 'isCurrentUser']);
}
}