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
|
|
|
|
87
|
|
|
//get the app source |
88
|
|
|
if ($source = Sources::getByTitle($app)) { |
89
|
|
|
|
90
|
|
|
//If source is apple verify if the token is valid |
91
|
|
|
if ($source->title == 'apple') { |
92
|
|
|
$deviceId = $this->validateAppleUser($deviceId)->sub; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$userSource = UserLinkedSources::findFirst([ |
96
|
|
|
'conditions' => 'users_id = ?0 AND source_users_id_text = ?1 AND source_id = ?2 AND is_deleted = 0', |
97
|
|
|
'bind' => [ |
98
|
|
|
$this->userData->getId(), |
99
|
|
|
$deviceId, |
100
|
|
|
$source->getId() |
101
|
|
|
] |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
if (!is_object($userSource)) { |
105
|
|
|
$userSource = new UserLinkedSources(); |
106
|
|
|
$userSource->users_id = $this->userData->getId(); |
107
|
|
|
$userSource->source_id = $source->getId(); |
108
|
|
|
$userSource->source_users_id = $this->userData->getId(); |
109
|
|
|
$userSource->source_users_id_text = $deviceId; |
110
|
|
|
$userSource->source_username = $this->userData->displayname . ' ' . $app; |
111
|
|
|
$userSource->is_deleted = 0; |
112
|
|
|
|
113
|
|
|
$userSource->saveOrFail(); |
114
|
|
|
|
115
|
|
|
$msg = 'User Device Associated'; |
116
|
|
|
} else { |
117
|
|
|
$msg = 'User Device Already Associated'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
//clean password @todo move this to a better place |
122
|
|
|
$this->userData->password = null; |
123
|
|
|
|
124
|
|
|
return $this->response([ |
125
|
|
|
'msg' => $msg, |
126
|
|
|
'user' => $this->userData |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Detach user's devices. |
132
|
|
|
* @param integer $id User's id |
133
|
|
|
* @param string $deviceId User's devices id |
134
|
|
|
* @return Response |
135
|
|
|
*/ |
136
|
|
|
public function detachDevice(int $id, string $deviceId): Response |
137
|
|
|
{ |
138
|
|
|
//$sourceId = $this->request->getPost('source_id', 'int'); |
139
|
|
|
$userSource = UserLinkedSources::findFirstOrFail([ |
140
|
|
|
'conditions' => 'users_id = ?0 and source_users_id_text = ?1 and is_deleted = 0', |
141
|
|
|
'bind' => [$this->userData->getId(), $deviceId] |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
$userSource->softDelete(); |
145
|
|
|
|
146
|
|
|
return $this->response([ |
147
|
|
|
'msg' => 'User Device detached', |
148
|
|
|
'user' => $this->userData |
149
|
|
|
]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Validate Apple User |
154
|
|
|
* @param string $identityToken |
155
|
|
|
* @return object |
156
|
|
|
*/ |
157
|
|
|
public function validateAppleUser(string $identityToken): object |
158
|
|
|
{ |
159
|
|
|
$appleUserInfo = ASDecoder::getAppleSignInPayload($identityToken); |
160
|
|
|
|
161
|
|
|
if (!is_object($appleUserInfo)) { |
162
|
|
|
throw new InternalServerErrorException('Apple user not valid'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $appleUserInfo; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths