1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Canvas\Api\Controllers; |
6
|
|
|
|
7
|
|
|
use Canvas\Models\UserLinkedSources; |
8
|
|
|
use Baka\Auth\Models\Sources; |
9
|
|
|
use Phalcon\Http\Response; |
10
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
11
|
|
|
use Canvas\Validation as CanvasValidation; |
12
|
|
|
use \Baka\ASDecoder; |
13
|
|
|
|
14
|
|
|
use Canvas\Http\Exception\InternalServerErrorException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class LanguagesController. |
18
|
|
|
* |
19
|
|
|
* @package Canvas\Api\Controllers |
20
|
|
|
* @property UserData $userData |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
class UserLinkedSourcesController extends BaseController |
24
|
|
|
{ |
25
|
|
|
/* |
26
|
|
|
* fields we accept to create |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $createFields = [ |
31
|
|
|
'users_id', |
32
|
|
|
'source_id', |
33
|
|
|
'source_users_id', |
34
|
|
|
'source_users_id_text', |
35
|
|
|
'source_username' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/* |
39
|
|
|
* fields we accept to create |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $updateFields = [ |
44
|
|
|
'users_id', |
45
|
|
|
'source_id', |
46
|
|
|
'source_users_id', |
47
|
|
|
'source_users_id_text', |
48
|
|
|
'source_username' |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* set objects. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function onConstruct() |
57
|
|
|
{ |
58
|
|
|
$this->model = new UserLinkedSources(); |
59
|
|
|
$this->softDelete = 1; |
60
|
|
|
$this->additionalSearchFields = [ |
61
|
|
|
['is_deleted', ':', '0'], |
62
|
|
|
['users_id', ':', $this->userData->getId()], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Associate a Device with the corrent loggedin user. |
68
|
|
|
* |
69
|
|
|
* @url /users/{id}/device |
70
|
|
|
* @method POST |
71
|
|
|
* @return Response |
72
|
|
|
*/ |
73
|
|
|
public function devices() : Response |
74
|
|
|
{ |
75
|
|
|
//Ok let validate user password |
76
|
|
|
$validation = new CanvasValidation(); |
77
|
|
|
$validation->add('app', new PresenceOf(['message' => _('App name is required.')])); |
78
|
|
|
$validation->add('deviceId', new PresenceOf(['message' => _('device ID is required.')])); |
79
|
|
|
$msg = null; |
80
|
|
|
|
81
|
|
|
//validate this form for password |
82
|
|
|
$validation->validate($this->request->getPost()); |
83
|
|
|
|
84
|
|
|
$app = $this->request->getPost('app', 'string'); |
85
|
|
|
$deviceId = $this->request->getPost('deviceId', 'string'); |
86
|
|
|
$de = $this->request->getPost('deviceId', 'string'); |
|
|
|
|
87
|
|
|
|
88
|
|
|
//get the app source |
89
|
|
|
if ($source = Sources::getByTitle($app)) { |
90
|
|
|
|
91
|
|
|
//If source is apple verify if the token is valid |
92
|
|
|
$appleUserInfo = $this->validateAppleUser($deviceId); |
93
|
|
|
|
94
|
|
|
if (!$appleUserInfo && $source->isApple()) { |
95
|
|
|
throw new InternalServerErrorException('Apple user not valid'); |
96
|
|
|
} else { |
97
|
|
|
$deviceId = $appleUserInfo->sub; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$userSource = UserLinkedSources::findFirst([ |
101
|
|
|
'conditions' => 'users_id = ?0 AND source_users_id_text = ?1 AND source_id = ?2 AND is_deleted = 0', |
102
|
|
|
'bind' => [ |
103
|
|
|
$this->userData->getId(), |
104
|
|
|
$deviceId, |
105
|
|
|
$source->getId() |
106
|
|
|
] |
107
|
|
|
]); |
108
|
|
|
|
109
|
|
|
if (!is_object($userSource)) { |
110
|
|
|
$userSource = new UserLinkedSources(); |
111
|
|
|
$userSource->users_id = $this->userData->getId(); |
112
|
|
|
$userSource->source_id = $source->getId(); |
113
|
|
|
$userSource->source_users_id = $this->userData->getId(); |
114
|
|
|
$userSource->source_users_id_text = $deviceId; |
115
|
|
|
$userSource->source_username = $this->userData->displayname . ' ' . $app; |
116
|
|
|
$userSource->is_deleted = 0; |
117
|
|
|
|
118
|
|
|
$userSource->saveOrFail(); |
119
|
|
|
|
120
|
|
|
$msg = 'User Device Associated'; |
121
|
|
|
} else { |
122
|
|
|
$msg = 'User Device Already Associated'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
//clean password @todo move this to a better place |
127
|
|
|
$this->userData->password = null; |
128
|
|
|
|
129
|
|
|
return $this->response([ |
130
|
|
|
'msg' => $msg, |
131
|
|
|
'user' => $this->userData |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Detach user's devices. |
137
|
|
|
* @param integer $id User's id |
138
|
|
|
* @param string $deviceId User's devices id |
139
|
|
|
* @return Response |
140
|
|
|
*/ |
141
|
|
|
public function detachDevice(int $id, string $deviceId): Response |
142
|
|
|
{ |
143
|
|
|
//$sourceId = $this->request->getPost('source_id', 'int'); |
144
|
|
|
$userSource = UserLinkedSources::findFirstOrFail([ |
145
|
|
|
'conditions' => 'users_id = ?0 and source_users_id_text = ?1 and is_deleted = 0', |
146
|
|
|
'bind' => [$this->userData->getId(), $deviceId] |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
$userSource->softDelete(); |
150
|
|
|
|
151
|
|
|
return $this->response([ |
152
|
|
|
'msg' => 'User Device detached', |
153
|
|
|
'user' => $this->userData |
154
|
|
|
]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Test Get Apple Access Tokens. |
159
|
|
|
*/ |
160
|
|
|
public function validateAppleUser(string $identityToken) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return is_object(ASDecoder::getAppleSignInPayload($identityToken)) ? ASDecoder::getAppleSignInPayload($identityToken) : false; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.