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