@@ -35,166 +35,166 @@ |
||
35 | 35 | |
36 | 36 | class Provider implements IProvider { |
37 | 37 | |
38 | - public const PASSWORD_CHANGED_BY = 'password_changed_by'; |
|
39 | - public const PASSWORD_CHANGED_SELF = 'password_changed_self'; |
|
40 | - public const PASSWORD_RESET = 'password_changed'; |
|
41 | - public const EMAIL_CHANGED_BY = 'email_changed_by'; |
|
42 | - public const EMAIL_CHANGED_SELF = 'email_changed_self'; |
|
43 | - public const EMAIL_CHANGED = 'email_changed'; |
|
44 | - public const APP_TOKEN_CREATED = 'app_token_created'; |
|
45 | - public const APP_TOKEN_UPDATED = 'app_token_updated'; |
|
46 | - public const APP_TOKEN_DELETED = 'app_token_deleted'; |
|
47 | - |
|
48 | - /** @var IFactory */ |
|
49 | - protected $languageFactory; |
|
50 | - |
|
51 | - /** @var IL10N */ |
|
52 | - protected $l; |
|
53 | - |
|
54 | - /** @var IURLGenerator */ |
|
55 | - protected $url; |
|
56 | - |
|
57 | - /** @var IUserManager */ |
|
58 | - protected $userManager; |
|
59 | - |
|
60 | - /** @var IManager */ |
|
61 | - private $activityManager; |
|
62 | - |
|
63 | - /** @var string[] cached displayNames - key is the UID and value the displayname */ |
|
64 | - protected $displayNames = []; |
|
65 | - |
|
66 | - public function __construct(IFactory $languageFactory, |
|
67 | - IURLGenerator $url, |
|
68 | - IUserManager $userManager, |
|
69 | - IManager $activityManager) { |
|
70 | - $this->languageFactory = $languageFactory; |
|
71 | - $this->url = $url; |
|
72 | - $this->userManager = $userManager; |
|
73 | - $this->activityManager = $activityManager; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param string $language |
|
78 | - * @param IEvent $event |
|
79 | - * @param IEvent|null $previousEvent |
|
80 | - * @return IEvent |
|
81 | - * @throws \InvalidArgumentException |
|
82 | - * @since 11.0.0 |
|
83 | - */ |
|
84 | - public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent { |
|
85 | - if ($event->getApp() !== 'settings') { |
|
86 | - throw new \InvalidArgumentException('Unknown app'); |
|
87 | - } |
|
88 | - |
|
89 | - $this->l = $this->languageFactory->get('settings', $language); |
|
90 | - |
|
91 | - if ($this->activityManager->getRequirePNG()) { |
|
92 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png'))); |
|
93 | - } else { |
|
94 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg'))); |
|
95 | - } |
|
96 | - |
|
97 | - if ($event->getSubject() === self::PASSWORD_CHANGED_BY) { |
|
98 | - $subject = $this->l->t('{actor} changed your password'); |
|
99 | - } else if ($event->getSubject() === self::PASSWORD_CHANGED_SELF) { |
|
100 | - $subject = $this->l->t('You changed your password'); |
|
101 | - } else if ($event->getSubject() === self::PASSWORD_RESET) { |
|
102 | - $subject = $this->l->t('Your password was reset by an administrator'); |
|
103 | - |
|
104 | - } else if ($event->getSubject() === self::EMAIL_CHANGED_BY) { |
|
105 | - $subject = $this->l->t('{actor} changed your email address'); |
|
106 | - } else if ($event->getSubject() === self::EMAIL_CHANGED_SELF) { |
|
107 | - $subject = $this->l->t('You changed your email address'); |
|
108 | - } else if ($event->getSubject() === self::EMAIL_CHANGED) { |
|
109 | - $subject = $this->l->t('Your email address was changed by an administrator'); |
|
110 | - |
|
111 | - } else if ($event->getSubject() === self::APP_TOKEN_CREATED) { |
|
112 | - $subject = $this->l->t('You created app password "{token}"'); |
|
113 | - } else if ($event->getSubject() === self::APP_TOKEN_UPDATED) { |
|
114 | - $subject = $this->l->t('You updated app password "{token}"'); |
|
115 | - } else if ($event->getSubject() === self::APP_TOKEN_DELETED) { |
|
116 | - $subject = $this->l->t('You deleted app password "{token}"'); |
|
117 | - |
|
118 | - } else { |
|
119 | - throw new \InvalidArgumentException('Unknown subject'); |
|
120 | - } |
|
121 | - |
|
122 | - $parsedParameters = $this->getParameters($event); |
|
123 | - $this->setSubjects($event, $subject, $parsedParameters); |
|
124 | - |
|
125 | - return $event; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param IEvent $event |
|
130 | - * @return array |
|
131 | - * @throws \InvalidArgumentException |
|
132 | - */ |
|
133 | - protected function getParameters(IEvent $event): array { |
|
134 | - $subject = $event->getSubject(); |
|
135 | - $parameters = $event->getSubjectParameters(); |
|
136 | - |
|
137 | - switch ($subject) { |
|
138 | - case self::PASSWORD_CHANGED_SELF: |
|
139 | - case self::PASSWORD_RESET: |
|
140 | - case self::EMAIL_CHANGED_SELF: |
|
141 | - case self::EMAIL_CHANGED: |
|
142 | - return []; |
|
143 | - case self::PASSWORD_CHANGED_BY: |
|
144 | - case self::EMAIL_CHANGED_BY: |
|
145 | - return [ |
|
146 | - 'actor' => $this->generateUserParameter($parameters[0]), |
|
147 | - ]; |
|
148 | - case self::APP_TOKEN_CREATED: |
|
149 | - case self::APP_TOKEN_UPDATED: |
|
150 | - case self::APP_TOKEN_DELETED: |
|
151 | - return [ |
|
152 | - 'token' => [ |
|
153 | - 'type' => 'highlight', |
|
154 | - 'id' => $event->getObjectId(), |
|
155 | - 'name' => $parameters[0], |
|
156 | - ] |
|
157 | - ]; |
|
158 | - } |
|
159 | - |
|
160 | - throw new \InvalidArgumentException('Unknown subject'); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @param IEvent $event |
|
165 | - * @param string $subject |
|
166 | - * @param array $parameters |
|
167 | - * @throws \InvalidArgumentException |
|
168 | - */ |
|
169 | - protected function setSubjects(IEvent $event, string $subject, array $parameters): void { |
|
170 | - $placeholders = $replacements = []; |
|
171 | - foreach ($parameters as $placeholder => $parameter) { |
|
172 | - $placeholders[] = '{' . $placeholder . '}'; |
|
173 | - $replacements[] = $parameter['name']; |
|
174 | - } |
|
175 | - |
|
176 | - $event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
|
177 | - ->setRichSubject($subject, $parameters); |
|
178 | - } |
|
179 | - |
|
180 | - protected function generateUserParameter(string $uid): array { |
|
181 | - if (!isset($this->displayNames[$uid])) { |
|
182 | - $this->displayNames[$uid] = $this->getDisplayName($uid); |
|
183 | - } |
|
184 | - |
|
185 | - return [ |
|
186 | - 'type' => 'user', |
|
187 | - 'id' => $uid, |
|
188 | - 'name' => $this->displayNames[$uid], |
|
189 | - ]; |
|
190 | - } |
|
191 | - |
|
192 | - protected function getDisplayName(string $uid): string { |
|
193 | - $user = $this->userManager->get($uid); |
|
194 | - if ($user instanceof IUser) { |
|
195 | - return $user->getDisplayName(); |
|
196 | - } |
|
197 | - |
|
198 | - return $uid; |
|
199 | - } |
|
38 | + public const PASSWORD_CHANGED_BY = 'password_changed_by'; |
|
39 | + public const PASSWORD_CHANGED_SELF = 'password_changed_self'; |
|
40 | + public const PASSWORD_RESET = 'password_changed'; |
|
41 | + public const EMAIL_CHANGED_BY = 'email_changed_by'; |
|
42 | + public const EMAIL_CHANGED_SELF = 'email_changed_self'; |
|
43 | + public const EMAIL_CHANGED = 'email_changed'; |
|
44 | + public const APP_TOKEN_CREATED = 'app_token_created'; |
|
45 | + public const APP_TOKEN_UPDATED = 'app_token_updated'; |
|
46 | + public const APP_TOKEN_DELETED = 'app_token_deleted'; |
|
47 | + |
|
48 | + /** @var IFactory */ |
|
49 | + protected $languageFactory; |
|
50 | + |
|
51 | + /** @var IL10N */ |
|
52 | + protected $l; |
|
53 | + |
|
54 | + /** @var IURLGenerator */ |
|
55 | + protected $url; |
|
56 | + |
|
57 | + /** @var IUserManager */ |
|
58 | + protected $userManager; |
|
59 | + |
|
60 | + /** @var IManager */ |
|
61 | + private $activityManager; |
|
62 | + |
|
63 | + /** @var string[] cached displayNames - key is the UID and value the displayname */ |
|
64 | + protected $displayNames = []; |
|
65 | + |
|
66 | + public function __construct(IFactory $languageFactory, |
|
67 | + IURLGenerator $url, |
|
68 | + IUserManager $userManager, |
|
69 | + IManager $activityManager) { |
|
70 | + $this->languageFactory = $languageFactory; |
|
71 | + $this->url = $url; |
|
72 | + $this->userManager = $userManager; |
|
73 | + $this->activityManager = $activityManager; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param string $language |
|
78 | + * @param IEvent $event |
|
79 | + * @param IEvent|null $previousEvent |
|
80 | + * @return IEvent |
|
81 | + * @throws \InvalidArgumentException |
|
82 | + * @since 11.0.0 |
|
83 | + */ |
|
84 | + public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent { |
|
85 | + if ($event->getApp() !== 'settings') { |
|
86 | + throw new \InvalidArgumentException('Unknown app'); |
|
87 | + } |
|
88 | + |
|
89 | + $this->l = $this->languageFactory->get('settings', $language); |
|
90 | + |
|
91 | + if ($this->activityManager->getRequirePNG()) { |
|
92 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png'))); |
|
93 | + } else { |
|
94 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg'))); |
|
95 | + } |
|
96 | + |
|
97 | + if ($event->getSubject() === self::PASSWORD_CHANGED_BY) { |
|
98 | + $subject = $this->l->t('{actor} changed your password'); |
|
99 | + } else if ($event->getSubject() === self::PASSWORD_CHANGED_SELF) { |
|
100 | + $subject = $this->l->t('You changed your password'); |
|
101 | + } else if ($event->getSubject() === self::PASSWORD_RESET) { |
|
102 | + $subject = $this->l->t('Your password was reset by an administrator'); |
|
103 | + |
|
104 | + } else if ($event->getSubject() === self::EMAIL_CHANGED_BY) { |
|
105 | + $subject = $this->l->t('{actor} changed your email address'); |
|
106 | + } else if ($event->getSubject() === self::EMAIL_CHANGED_SELF) { |
|
107 | + $subject = $this->l->t('You changed your email address'); |
|
108 | + } else if ($event->getSubject() === self::EMAIL_CHANGED) { |
|
109 | + $subject = $this->l->t('Your email address was changed by an administrator'); |
|
110 | + |
|
111 | + } else if ($event->getSubject() === self::APP_TOKEN_CREATED) { |
|
112 | + $subject = $this->l->t('You created app password "{token}"'); |
|
113 | + } else if ($event->getSubject() === self::APP_TOKEN_UPDATED) { |
|
114 | + $subject = $this->l->t('You updated app password "{token}"'); |
|
115 | + } else if ($event->getSubject() === self::APP_TOKEN_DELETED) { |
|
116 | + $subject = $this->l->t('You deleted app password "{token}"'); |
|
117 | + |
|
118 | + } else { |
|
119 | + throw new \InvalidArgumentException('Unknown subject'); |
|
120 | + } |
|
121 | + |
|
122 | + $parsedParameters = $this->getParameters($event); |
|
123 | + $this->setSubjects($event, $subject, $parsedParameters); |
|
124 | + |
|
125 | + return $event; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param IEvent $event |
|
130 | + * @return array |
|
131 | + * @throws \InvalidArgumentException |
|
132 | + */ |
|
133 | + protected function getParameters(IEvent $event): array { |
|
134 | + $subject = $event->getSubject(); |
|
135 | + $parameters = $event->getSubjectParameters(); |
|
136 | + |
|
137 | + switch ($subject) { |
|
138 | + case self::PASSWORD_CHANGED_SELF: |
|
139 | + case self::PASSWORD_RESET: |
|
140 | + case self::EMAIL_CHANGED_SELF: |
|
141 | + case self::EMAIL_CHANGED: |
|
142 | + return []; |
|
143 | + case self::PASSWORD_CHANGED_BY: |
|
144 | + case self::EMAIL_CHANGED_BY: |
|
145 | + return [ |
|
146 | + 'actor' => $this->generateUserParameter($parameters[0]), |
|
147 | + ]; |
|
148 | + case self::APP_TOKEN_CREATED: |
|
149 | + case self::APP_TOKEN_UPDATED: |
|
150 | + case self::APP_TOKEN_DELETED: |
|
151 | + return [ |
|
152 | + 'token' => [ |
|
153 | + 'type' => 'highlight', |
|
154 | + 'id' => $event->getObjectId(), |
|
155 | + 'name' => $parameters[0], |
|
156 | + ] |
|
157 | + ]; |
|
158 | + } |
|
159 | + |
|
160 | + throw new \InvalidArgumentException('Unknown subject'); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @param IEvent $event |
|
165 | + * @param string $subject |
|
166 | + * @param array $parameters |
|
167 | + * @throws \InvalidArgumentException |
|
168 | + */ |
|
169 | + protected function setSubjects(IEvent $event, string $subject, array $parameters): void { |
|
170 | + $placeholders = $replacements = []; |
|
171 | + foreach ($parameters as $placeholder => $parameter) { |
|
172 | + $placeholders[] = '{' . $placeholder . '}'; |
|
173 | + $replacements[] = $parameter['name']; |
|
174 | + } |
|
175 | + |
|
176 | + $event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
|
177 | + ->setRichSubject($subject, $parameters); |
|
178 | + } |
|
179 | + |
|
180 | + protected function generateUserParameter(string $uid): array { |
|
181 | + if (!isset($this->displayNames[$uid])) { |
|
182 | + $this->displayNames[$uid] = $this->getDisplayName($uid); |
|
183 | + } |
|
184 | + |
|
185 | + return [ |
|
186 | + 'type' => 'user', |
|
187 | + 'id' => $uid, |
|
188 | + 'name' => $this->displayNames[$uid], |
|
189 | + ]; |
|
190 | + } |
|
191 | + |
|
192 | + protected function getDisplayName(string $uid): string { |
|
193 | + $user = $this->userManager->get($uid); |
|
194 | + if ($user instanceof IUser) { |
|
195 | + return $user->getDisplayName(); |
|
196 | + } |
|
197 | + |
|
198 | + return $uid; |
|
199 | + } |
|
200 | 200 | } |
@@ -45,226 +45,226 @@ |
||
45 | 45 | |
46 | 46 | class AuthSettingsController extends Controller { |
47 | 47 | |
48 | - /** @var IProvider */ |
|
49 | - private $tokenProvider; |
|
48 | + /** @var IProvider */ |
|
49 | + private $tokenProvider; |
|
50 | 50 | |
51 | - /** @var ISession */ |
|
52 | - private $session; |
|
51 | + /** @var ISession */ |
|
52 | + private $session; |
|
53 | 53 | |
54 | - /** @var string */ |
|
55 | - private $uid; |
|
54 | + /** @var string */ |
|
55 | + private $uid; |
|
56 | 56 | |
57 | - /** @var ISecureRandom */ |
|
58 | - private $random; |
|
57 | + /** @var ISecureRandom */ |
|
58 | + private $random; |
|
59 | 59 | |
60 | - /** @var IManager */ |
|
61 | - private $activityManager; |
|
60 | + /** @var IManager */ |
|
61 | + private $activityManager; |
|
62 | 62 | |
63 | - /** @var ILogger */ |
|
64 | - private $logger; |
|
63 | + /** @var ILogger */ |
|
64 | + private $logger; |
|
65 | 65 | |
66 | - /** |
|
67 | - * @param string $appName |
|
68 | - * @param IRequest $request |
|
69 | - * @param IProvider $tokenProvider |
|
70 | - * @param ISession $session |
|
71 | - * @param ISecureRandom $random |
|
72 | - * @param string|null $userId |
|
73 | - * @param IManager $activityManager |
|
74 | - * @param ILogger $logger |
|
75 | - */ |
|
76 | - public function __construct(string $appName, |
|
77 | - IRequest $request, |
|
78 | - IProvider $tokenProvider, |
|
79 | - ISession $session, |
|
80 | - ISecureRandom $random, |
|
81 | - ?string $userId, |
|
82 | - IManager $activityManager, |
|
83 | - ILogger $logger) { |
|
84 | - parent::__construct($appName, $request); |
|
85 | - $this->tokenProvider = $tokenProvider; |
|
86 | - $this->uid = $userId; |
|
87 | - $this->session = $session; |
|
88 | - $this->random = $random; |
|
89 | - $this->activityManager = $activityManager; |
|
90 | - $this->logger = $logger; |
|
91 | - } |
|
66 | + /** |
|
67 | + * @param string $appName |
|
68 | + * @param IRequest $request |
|
69 | + * @param IProvider $tokenProvider |
|
70 | + * @param ISession $session |
|
71 | + * @param ISecureRandom $random |
|
72 | + * @param string|null $userId |
|
73 | + * @param IManager $activityManager |
|
74 | + * @param ILogger $logger |
|
75 | + */ |
|
76 | + public function __construct(string $appName, |
|
77 | + IRequest $request, |
|
78 | + IProvider $tokenProvider, |
|
79 | + ISession $session, |
|
80 | + ISecureRandom $random, |
|
81 | + ?string $userId, |
|
82 | + IManager $activityManager, |
|
83 | + ILogger $logger) { |
|
84 | + parent::__construct($appName, $request); |
|
85 | + $this->tokenProvider = $tokenProvider; |
|
86 | + $this->uid = $userId; |
|
87 | + $this->session = $session; |
|
88 | + $this->random = $random; |
|
89 | + $this->activityManager = $activityManager; |
|
90 | + $this->logger = $logger; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * @NoAdminRequired |
|
95 | - * @NoSubadminRequired |
|
96 | - * |
|
97 | - * @return JSONResponse|array |
|
98 | - */ |
|
99 | - public function index() { |
|
100 | - $tokens = $this->tokenProvider->getTokenByUser($this->uid); |
|
93 | + /** |
|
94 | + * @NoAdminRequired |
|
95 | + * @NoSubadminRequired |
|
96 | + * |
|
97 | + * @return JSONResponse|array |
|
98 | + */ |
|
99 | + public function index() { |
|
100 | + $tokens = $this->tokenProvider->getTokenByUser($this->uid); |
|
101 | 101 | |
102 | - try { |
|
103 | - $sessionId = $this->session->getId(); |
|
104 | - } catch (SessionNotAvailableException $ex) { |
|
105 | - return $this->getServiceNotAvailableResponse(); |
|
106 | - } |
|
107 | - try { |
|
108 | - $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
109 | - } catch (InvalidTokenException $ex) { |
|
110 | - return $this->getServiceNotAvailableResponse(); |
|
111 | - } |
|
102 | + try { |
|
103 | + $sessionId = $this->session->getId(); |
|
104 | + } catch (SessionNotAvailableException $ex) { |
|
105 | + return $this->getServiceNotAvailableResponse(); |
|
106 | + } |
|
107 | + try { |
|
108 | + $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
109 | + } catch (InvalidTokenException $ex) { |
|
110 | + return $this->getServiceNotAvailableResponse(); |
|
111 | + } |
|
112 | 112 | |
113 | - return array_map(function (IToken $token) use ($sessionToken) { |
|
114 | - $data = $token->jsonSerialize(); |
|
115 | - if ($sessionToken->getId() === $token->getId()) { |
|
116 | - $data['canDelete'] = false; |
|
117 | - $data['current'] = true; |
|
118 | - } else { |
|
119 | - $data['canDelete'] = true; |
|
120 | - } |
|
121 | - return $data; |
|
122 | - }, $tokens); |
|
123 | - } |
|
113 | + return array_map(function (IToken $token) use ($sessionToken) { |
|
114 | + $data = $token->jsonSerialize(); |
|
115 | + if ($sessionToken->getId() === $token->getId()) { |
|
116 | + $data['canDelete'] = false; |
|
117 | + $data['current'] = true; |
|
118 | + } else { |
|
119 | + $data['canDelete'] = true; |
|
120 | + } |
|
121 | + return $data; |
|
122 | + }, $tokens); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * @NoAdminRequired |
|
127 | - * @NoSubadminRequired |
|
128 | - * @PasswordConfirmationRequired |
|
129 | - * |
|
130 | - * @param string $name |
|
131 | - * @return JSONResponse |
|
132 | - */ |
|
133 | - public function create($name) { |
|
134 | - try { |
|
135 | - $sessionId = $this->session->getId(); |
|
136 | - } catch (SessionNotAvailableException $ex) { |
|
137 | - return $this->getServiceNotAvailableResponse(); |
|
138 | - } |
|
125 | + /** |
|
126 | + * @NoAdminRequired |
|
127 | + * @NoSubadminRequired |
|
128 | + * @PasswordConfirmationRequired |
|
129 | + * |
|
130 | + * @param string $name |
|
131 | + * @return JSONResponse |
|
132 | + */ |
|
133 | + public function create($name) { |
|
134 | + try { |
|
135 | + $sessionId = $this->session->getId(); |
|
136 | + } catch (SessionNotAvailableException $ex) { |
|
137 | + return $this->getServiceNotAvailableResponse(); |
|
138 | + } |
|
139 | 139 | |
140 | - try { |
|
141 | - $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
142 | - $loginName = $sessionToken->getLoginName(); |
|
143 | - try { |
|
144 | - $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
145 | - } catch (PasswordlessTokenException $ex) { |
|
146 | - $password = null; |
|
147 | - } |
|
148 | - } catch (InvalidTokenException $ex) { |
|
149 | - return $this->getServiceNotAvailableResponse(); |
|
150 | - } |
|
140 | + try { |
|
141 | + $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
142 | + $loginName = $sessionToken->getLoginName(); |
|
143 | + try { |
|
144 | + $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
145 | + } catch (PasswordlessTokenException $ex) { |
|
146 | + $password = null; |
|
147 | + } |
|
148 | + } catch (InvalidTokenException $ex) { |
|
149 | + return $this->getServiceNotAvailableResponse(); |
|
150 | + } |
|
151 | 151 | |
152 | - $token = $this->generateRandomDeviceToken(); |
|
153 | - $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN); |
|
154 | - $tokenData = $deviceToken->jsonSerialize(); |
|
155 | - $tokenData['canDelete'] = true; |
|
152 | + $token = $this->generateRandomDeviceToken(); |
|
153 | + $deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN); |
|
154 | + $tokenData = $deviceToken->jsonSerialize(); |
|
155 | + $tokenData['canDelete'] = true; |
|
156 | 156 | |
157 | - $this->publishActivity(Provider::APP_TOKEN_CREATED, $deviceToken->getId(), $deviceToken->getName()); |
|
157 | + $this->publishActivity(Provider::APP_TOKEN_CREATED, $deviceToken->getId(), $deviceToken->getName()); |
|
158 | 158 | |
159 | - return new JSONResponse([ |
|
160 | - 'token' => $token, |
|
161 | - 'loginName' => $loginName, |
|
162 | - 'deviceToken' => $tokenData, |
|
163 | - ]); |
|
164 | - } |
|
159 | + return new JSONResponse([ |
|
160 | + 'token' => $token, |
|
161 | + 'loginName' => $loginName, |
|
162 | + 'deviceToken' => $tokenData, |
|
163 | + ]); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @return JSONResponse |
|
168 | - */ |
|
169 | - private function getServiceNotAvailableResponse() { |
|
170 | - $resp = new JSONResponse(); |
|
171 | - $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); |
|
172 | - return $resp; |
|
173 | - } |
|
166 | + /** |
|
167 | + * @return JSONResponse |
|
168 | + */ |
|
169 | + private function getServiceNotAvailableResponse() { |
|
170 | + $resp = new JSONResponse(); |
|
171 | + $resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); |
|
172 | + return $resp; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Return a 25 digit device password |
|
177 | - * |
|
178 | - * Example: AbCdE-fGhJk-MnPqR-sTwXy-23456 |
|
179 | - * |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - private function generateRandomDeviceToken() { |
|
183 | - $groups = []; |
|
184 | - for ($i = 0; $i < 5; $i++) { |
|
185 | - $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE); |
|
186 | - } |
|
187 | - return implode('-', $groups); |
|
188 | - } |
|
175 | + /** |
|
176 | + * Return a 25 digit device password |
|
177 | + * |
|
178 | + * Example: AbCdE-fGhJk-MnPqR-sTwXy-23456 |
|
179 | + * |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + private function generateRandomDeviceToken() { |
|
183 | + $groups = []; |
|
184 | + for ($i = 0; $i < 5; $i++) { |
|
185 | + $groups[] = $this->random->generate(5, ISecureRandom::CHAR_HUMAN_READABLE); |
|
186 | + } |
|
187 | + return implode('-', $groups); |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * @NoAdminRequired |
|
192 | - * @NoSubadminRequired |
|
193 | - * |
|
194 | - * @param int $id |
|
195 | - * @return array|JSONResponse |
|
196 | - */ |
|
197 | - public function destroy($id) { |
|
198 | - try { |
|
199 | - $token = $this->findTokenByIdAndUser($id); |
|
200 | - } catch (InvalidTokenException $e) { |
|
201 | - return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
202 | - } |
|
190 | + /** |
|
191 | + * @NoAdminRequired |
|
192 | + * @NoSubadminRequired |
|
193 | + * |
|
194 | + * @param int $id |
|
195 | + * @return array|JSONResponse |
|
196 | + */ |
|
197 | + public function destroy($id) { |
|
198 | + try { |
|
199 | + $token = $this->findTokenByIdAndUser($id); |
|
200 | + } catch (InvalidTokenException $e) { |
|
201 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
202 | + } |
|
203 | 203 | |
204 | - $this->tokenProvider->invalidateTokenById($this->uid, $token->getId()); |
|
205 | - $this->publishActivity(Provider::APP_TOKEN_DELETED, $token->getId(), $token->getName()); |
|
206 | - return []; |
|
207 | - } |
|
204 | + $this->tokenProvider->invalidateTokenById($this->uid, $token->getId()); |
|
205 | + $this->publishActivity(Provider::APP_TOKEN_DELETED, $token->getId(), $token->getName()); |
|
206 | + return []; |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * @NoAdminRequired |
|
211 | - * @NoSubadminRequired |
|
212 | - * |
|
213 | - * @param int $id |
|
214 | - * @param array $scope |
|
215 | - * @return array|JSONResponse |
|
216 | - */ |
|
217 | - public function update($id, array $scope) { |
|
218 | - try { |
|
219 | - $token = $this->findTokenByIdAndUser($id); |
|
220 | - } catch (InvalidTokenException $e) { |
|
221 | - return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
222 | - } |
|
209 | + /** |
|
210 | + * @NoAdminRequired |
|
211 | + * @NoSubadminRequired |
|
212 | + * |
|
213 | + * @param int $id |
|
214 | + * @param array $scope |
|
215 | + * @return array|JSONResponse |
|
216 | + */ |
|
217 | + public function update($id, array $scope) { |
|
218 | + try { |
|
219 | + $token = $this->findTokenByIdAndUser($id); |
|
220 | + } catch (InvalidTokenException $e) { |
|
221 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
222 | + } |
|
223 | 223 | |
224 | - $token->setScope([ |
|
225 | - 'filesystem' => $scope['filesystem'] |
|
226 | - ]); |
|
224 | + $token->setScope([ |
|
225 | + 'filesystem' => $scope['filesystem'] |
|
226 | + ]); |
|
227 | 227 | |
228 | - $this->tokenProvider->updateToken($token); |
|
229 | - $this->publishActivity(Provider::APP_TOKEN_UPDATED, $token->getId(), $token->getName()); |
|
230 | - return []; |
|
231 | - } |
|
228 | + $this->tokenProvider->updateToken($token); |
|
229 | + $this->publishActivity(Provider::APP_TOKEN_UPDATED, $token->getId(), $token->getName()); |
|
230 | + return []; |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * @param string $subject |
|
235 | - * @param int $id |
|
236 | - * @param string|null $tokenName |
|
237 | - */ |
|
238 | - private function publishActivity(string $subject, int $id, ?string $tokenName = null): void { |
|
239 | - $event = $this->activityManager->generateEvent(); |
|
240 | - $event->setApp('settings') |
|
241 | - ->setType('security') |
|
242 | - ->setAffectedUser($this->uid) |
|
243 | - ->setAuthor($this->uid) |
|
244 | - ->setSubject($subject, [$tokenName]) |
|
245 | - ->setObject('app_token', $id, 'App Password'); |
|
233 | + /** |
|
234 | + * @param string $subject |
|
235 | + * @param int $id |
|
236 | + * @param string|null $tokenName |
|
237 | + */ |
|
238 | + private function publishActivity(string $subject, int $id, ?string $tokenName = null): void { |
|
239 | + $event = $this->activityManager->generateEvent(); |
|
240 | + $event->setApp('settings') |
|
241 | + ->setType('security') |
|
242 | + ->setAffectedUser($this->uid) |
|
243 | + ->setAuthor($this->uid) |
|
244 | + ->setSubject($subject, [$tokenName]) |
|
245 | + ->setObject('app_token', $id, 'App Password'); |
|
246 | 246 | |
247 | - try { |
|
248 | - $this->activityManager->publish($event); |
|
249 | - } catch (BadMethodCallException $e) { |
|
250 | - $this->logger->warning('could not publish activity'); |
|
251 | - $this->logger->logException($e); |
|
252 | - } |
|
253 | - } |
|
247 | + try { |
|
248 | + $this->activityManager->publish($event); |
|
249 | + } catch (BadMethodCallException $e) { |
|
250 | + $this->logger->warning('could not publish activity'); |
|
251 | + $this->logger->logException($e); |
|
252 | + } |
|
253 | + } |
|
254 | 254 | |
255 | - /** |
|
256 | - * Find a token by given id and check if uid for current session belongs to this token |
|
257 | - * |
|
258 | - * @param int $id |
|
259 | - * @return IToken |
|
260 | - * @throws InvalidTokenException |
|
261 | - * @throws \OC\Authentication\Exceptions\ExpiredTokenException |
|
262 | - */ |
|
263 | - private function findTokenByIdAndUser(int $id): IToken { |
|
264 | - $token = $this->tokenProvider->getTokenById($id); |
|
265 | - if ($token->getUID() !== $this->uid) { |
|
266 | - throw new InvalidTokenException('This token does not belong to you!'); |
|
267 | - } |
|
268 | - return $token; |
|
269 | - } |
|
255 | + /** |
|
256 | + * Find a token by given id and check if uid for current session belongs to this token |
|
257 | + * |
|
258 | + * @param int $id |
|
259 | + * @return IToken |
|
260 | + * @throws InvalidTokenException |
|
261 | + * @throws \OC\Authentication\Exceptions\ExpiredTokenException |
|
262 | + */ |
|
263 | + private function findTokenByIdAndUser(int $id): IToken { |
|
264 | + $token = $this->tokenProvider->getTokenById($id); |
|
265 | + if ($token->getUID() !== $this->uid) { |
|
266 | + throw new InvalidTokenException('This token does not belong to you!'); |
|
267 | + } |
|
268 | + return $token; |
|
269 | + } |
|
270 | 270 | } |