1 | <?php |
||
2 | |||
3 | namespace Apps\Model\Front\User; |
||
4 | |||
5 | use Apps\ActiveRecord\UserProvider; |
||
6 | use Apps\Model\Front\Profile\FormAvatarUpload; |
||
7 | use Ffcms\Core\App; |
||
8 | use Ffcms\Core\Exception\ForbiddenException; |
||
9 | use Ffcms\Core\Helper\FileSystem\File; |
||
10 | use Ffcms\Core\Helper\Type\Arr; |
||
11 | use Ffcms\Core\Helper\Type\Str; |
||
12 | use Ffcms\Core\Helper\Url; |
||
0 ignored issues
–
show
|
|||
13 | use Ffcms\Core\Interfaces\iUser; |
||
14 | use Symfony\Component\HttpFoundation\File\File as FileObject; |
||
15 | |||
16 | /** |
||
17 | * Class FormSocialAuth. Model of social authorization and registering on top layer of register model. |
||
18 | * @package Apps\Model\Front\User |
||
19 | */ |
||
20 | class FormSocialAuth extends FormRegister |
||
21 | { |
||
22 | /** @var string */ |
||
23 | public $profileLink; |
||
24 | |||
25 | /** @var string */ |
||
26 | private $_provider_name; |
||
27 | private $_provider_id; |
||
28 | /** @var \Hybrid_User_Profile */ |
||
29 | private $_identity; |
||
30 | /** @var UserProvider */ |
||
31 | private $_record; |
||
32 | |||
33 | /** |
||
34 | * FormSocialAuth constructor. Pass provider name as string and identity as object of hybrid auth |
||
35 | * @param bool $provider |
||
36 | * @param $identity |
||
37 | */ |
||
38 | public function __construct($provider, $identity) |
||
39 | { |
||
40 | $this->_provider_name = (string)$provider; |
||
41 | $this->_identity = $identity; |
||
42 | parent::__construct(false); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Parse user identifier to attributes |
||
47 | */ |
||
48 | public function before() |
||
49 | { |
||
50 | // set unique user id from provider response |
||
51 | $this->_provider_id = $this->_identity->identifier; |
||
52 | |||
53 | // grab some data from identity provider |
||
54 | if ($this->email === null) { |
||
55 | $this->email = $this->_identity->email; |
||
56 | } |
||
57 | $this->profileLink = $this->_identity->profileURL; |
||
58 | |||
59 | // get record info from db for this identifier if exists |
||
60 | $this->_record = UserProvider::where('provider_name', '=', $this->_provider_name) |
||
61 | ->where('provider_id', '=', $this->_provider_id) |
||
62 | ->first(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Check if this identity always exists |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function identityExists() |
||
70 | { |
||
71 | return ($this->_record !== null && $this->_record->count() === 1); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Make user authorization from social identity to website session |
||
76 | * @return bool |
||
77 | * @throws \Ffcms\Core\Exception\ForbiddenException |
||
78 | */ |
||
79 | public function makeAuth() |
||
80 | { |
||
81 | if ($this->_record === null) { |
||
82 | return false; |
||
83 | } |
||
84 | // get user from belongsTo relation |
||
85 | $user = $this->_record->user; |
||
86 | // maybe user was deleted without data provider record? |
||
87 | if (!$user instanceof iUser) { |
||
88 | throw new ForbiddenException(__('User related to this social account was deleted')); |
||
89 | } |
||
90 | // initialize login model |
||
91 | $loginModel = new FormLogin(); |
||
92 | // open session & return status |
||
93 | return $loginModel->openSession($user); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Override default registration function with social auth data compatability |
||
98 | * @param bool $activation |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function tryRegister($activation = false) |
||
102 | { |
||
103 | // try to complete register process |
||
104 | $success = parent::tryRegister($activation); |
||
105 | if ($success && $this->_userObject !== null) { |
||
106 | // save remote auth data to relation table |
||
107 | $provider = new UserProvider(); |
||
108 | $provider->provider_name = $this->_provider_name; |
||
109 | $provider->provider_id = $this->_provider_id; |
||
110 | $provider->user_id = $this->_userObject->id; |
||
111 | $provider->save(); |
||
112 | |||
113 | // get profile object from attr |
||
114 | $profile = $this->_profileObject; |
||
115 | // set nickname from remote service |
||
116 | if (!Str::likeEmpty($this->_identity->displayName)) { |
||
117 | $profile->nick = $this->_identity->displayName; |
||
118 | } |
||
119 | // set profile as user website |
||
120 | $profile->url = $this->_identity->profileURL; |
||
121 | // try to get gender (sex) |
||
122 | if ($this->_identity->gender !== null) { |
||
123 | $profile->sex = $this->_identity->gender === 'female' ? 2 : 1; |
||
124 | } |
||
125 | // set birthday if available |
||
126 | if ($this->_identity->birthDay !== null && $this->_identity->birthMonth !== null && $this->_identity->birthYear !== null) { |
||
127 | $profile->birthday = $this->_identity->birthYear . '-' . $this->_identity->birthMonth . '-' . $this->_identity->birthDay; |
||
128 | } |
||
129 | |||
130 | // try to parse avatar from remote service |
||
131 | if ($this->_identity->photoURL !== null) { |
||
132 | $this->parseAvatar($this->_identity->photoURL, $this->_userObject->id); |
||
133 | } |
||
134 | |||
135 | // save profile data |
||
136 | $profile->save(); |
||
137 | } |
||
138 | |||
139 | return $success; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Try to download and parse remote avatar |
||
144 | * @param string $url |
||
145 | * @param int $userId |
||
146 | */ |
||
147 | protected function parseAvatar($url, $userId) |
||
148 | { |
||
149 | // check if user is defined |
||
150 | if ((int)$userId < 1) { |
||
151 | return; |
||
152 | } |
||
153 | |||
154 | // check remote image extension |
||
155 | $imageExtension = Str::lastIn($url, '.', true); |
||
156 | if (!Arr::in($imageExtension, ['png', 'gif', 'jpg', 'jpeg'])) { |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | // try to get image binary data |
||
161 | $imageContent = File::getFromUrl($url); |
||
162 | if ($imageContent === null || Str::likeEmpty($imageContent)) { |
||
163 | return; |
||
164 | } |
||
165 | |||
166 | // write image to filesystem |
||
167 | $imagePath = '/upload/user/avatar/original/' . $userId . '.' . $imageExtension; |
||
168 | $write = File::write($imagePath, $imageContent); |
||
169 | if ($write === false) { |
||
170 | return; |
||
171 | } |
||
172 | |||
173 | // try to write and resize file |
||
174 | try { |
||
175 | $fileObject = new FileObject(root . $imagePath); |
||
176 | $avatarUpload = new FormAvatarUpload(); |
||
177 | $avatarUpload->resizeAndSave($fileObject, $userId, 'small'); |
||
178 | $avatarUpload->resizeAndSave($fileObject, $userId, 'medium'); |
||
179 | $avatarUpload->resizeAndSave($fileObject, $userId, 'big'); |
||
180 | } catch (\Exception $e) { |
||
181 | if (App::$Debug) { |
||
182 | App::$Debug->addException($e); |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 |
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