@@ -71,7 +71,7 @@ |
||
71 | 71 | |
72 | 72 | if ($response_type !== 'code') { |
73 | 73 | //Fail |
74 | - $url = $client->getRedirectUri() . '?error=unsupported_response_type&state=' . $state; |
|
74 | + $url = $client->getRedirectUri().'?error=unsupported_response_type&state='.$state; |
|
75 | 75 | return new RedirectResponse($url); |
76 | 76 | } |
77 | 77 |
@@ -39,72 +39,72 @@ |
||
39 | 39 | use OCP\IURLGenerator; |
40 | 40 | |
41 | 41 | class LoginRedirectorController extends Controller { |
42 | - /** @var IURLGenerator */ |
|
43 | - private $urlGenerator; |
|
44 | - /** @var ClientMapper */ |
|
45 | - private $clientMapper; |
|
46 | - /** @var ISession */ |
|
47 | - private $session; |
|
48 | - /** @var IL10N */ |
|
49 | - private $l; |
|
42 | + /** @var IURLGenerator */ |
|
43 | + private $urlGenerator; |
|
44 | + /** @var ClientMapper */ |
|
45 | + private $clientMapper; |
|
46 | + /** @var ISession */ |
|
47 | + private $session; |
|
48 | + /** @var IL10N */ |
|
49 | + private $l; |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param string $appName |
|
53 | - * @param IRequest $request |
|
54 | - * @param IURLGenerator $urlGenerator |
|
55 | - * @param ClientMapper $clientMapper |
|
56 | - * @param ISession $session |
|
57 | - * @param IL10N $l |
|
58 | - */ |
|
59 | - public function __construct(string $appName, |
|
60 | - IRequest $request, |
|
61 | - IURLGenerator $urlGenerator, |
|
62 | - ClientMapper $clientMapper, |
|
63 | - ISession $session, |
|
64 | - IL10N $l) { |
|
65 | - parent::__construct($appName, $request); |
|
66 | - $this->urlGenerator = $urlGenerator; |
|
67 | - $this->clientMapper = $clientMapper; |
|
68 | - $this->session = $session; |
|
69 | - $this->l = $l; |
|
70 | - } |
|
51 | + /** |
|
52 | + * @param string $appName |
|
53 | + * @param IRequest $request |
|
54 | + * @param IURLGenerator $urlGenerator |
|
55 | + * @param ClientMapper $clientMapper |
|
56 | + * @param ISession $session |
|
57 | + * @param IL10N $l |
|
58 | + */ |
|
59 | + public function __construct(string $appName, |
|
60 | + IRequest $request, |
|
61 | + IURLGenerator $urlGenerator, |
|
62 | + ClientMapper $clientMapper, |
|
63 | + ISession $session, |
|
64 | + IL10N $l) { |
|
65 | + parent::__construct($appName, $request); |
|
66 | + $this->urlGenerator = $urlGenerator; |
|
67 | + $this->clientMapper = $clientMapper; |
|
68 | + $this->session = $session; |
|
69 | + $this->l = $l; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @PublicPage |
|
74 | - * @NoCSRFRequired |
|
75 | - * @UseSession |
|
76 | - * |
|
77 | - * @param string $client_id |
|
78 | - * @param string $state |
|
79 | - * @param string $response_type |
|
80 | - * @return Response |
|
81 | - */ |
|
82 | - public function authorize($client_id, |
|
83 | - $state, |
|
84 | - $response_type): Response { |
|
85 | - try { |
|
86 | - $client = $this->clientMapper->getByIdentifier($client_id); |
|
87 | - } catch (ClientNotFoundException $e) { |
|
88 | - $params = [ |
|
89 | - 'content' => $this->l->t('Your client is not authorized to connect. Please inform the administrator of your client.'), |
|
90 | - ]; |
|
91 | - return new TemplateResponse('core', '404', $params, 'guest'); |
|
92 | - } |
|
72 | + /** |
|
73 | + * @PublicPage |
|
74 | + * @NoCSRFRequired |
|
75 | + * @UseSession |
|
76 | + * |
|
77 | + * @param string $client_id |
|
78 | + * @param string $state |
|
79 | + * @param string $response_type |
|
80 | + * @return Response |
|
81 | + */ |
|
82 | + public function authorize($client_id, |
|
83 | + $state, |
|
84 | + $response_type): Response { |
|
85 | + try { |
|
86 | + $client = $this->clientMapper->getByIdentifier($client_id); |
|
87 | + } catch (ClientNotFoundException $e) { |
|
88 | + $params = [ |
|
89 | + 'content' => $this->l->t('Your client is not authorized to connect. Please inform the administrator of your client.'), |
|
90 | + ]; |
|
91 | + return new TemplateResponse('core', '404', $params, 'guest'); |
|
92 | + } |
|
93 | 93 | |
94 | - if ($response_type !== 'code') { |
|
95 | - //Fail |
|
96 | - $url = $client->getRedirectUri() . '?error=unsupported_response_type&state=' . $state; |
|
97 | - return new RedirectResponse($url); |
|
98 | - } |
|
94 | + if ($response_type !== 'code') { |
|
95 | + //Fail |
|
96 | + $url = $client->getRedirectUri() . '?error=unsupported_response_type&state=' . $state; |
|
97 | + return new RedirectResponse($url); |
|
98 | + } |
|
99 | 99 | |
100 | - $this->session->set('oauth.state', $state); |
|
100 | + $this->session->set('oauth.state', $state); |
|
101 | 101 | |
102 | - $targetUrl = $this->urlGenerator->linkToRouteAbsolute( |
|
103 | - 'core.ClientFlowLogin.showAuthPickerPage', |
|
104 | - [ |
|
105 | - 'clientIdentifier' => $client->getClientIdentifier(), |
|
106 | - ] |
|
107 | - ); |
|
108 | - return new RedirectResponse($targetUrl); |
|
109 | - } |
|
102 | + $targetUrl = $this->urlGenerator->linkToRouteAbsolute( |
|
103 | + 'core.ClientFlowLogin.showAuthPickerPage', |
|
104 | + [ |
|
105 | + 'clientIdentifier' => $client->getClientIdentifier(), |
|
106 | + ] |
|
107 | + ); |
|
108 | + return new RedirectResponse($targetUrl); |
|
109 | + } |
|
110 | 110 | } |
@@ -25,10 +25,10 @@ |
||
25 | 25 | use OCP\IL10N; |
26 | 26 | |
27 | 27 | class KerberosAuth extends AuthMechanism { |
28 | - public function __construct(IL10N $l) { |
|
29 | - $this |
|
30 | - ->setIdentifier('smb::kerberos') |
|
31 | - ->setScheme(self::SCHEME_SMB) |
|
32 | - ->setText($l->t('Kerberos ticket')); |
|
33 | - } |
|
28 | + public function __construct(IL10N $l) { |
|
29 | + $this |
|
30 | + ->setIdentifier('smb::kerberos') |
|
31 | + ->setScheme(self::SCHEME_SMB) |
|
32 | + ->setText($l->t('Kerberos ticket')); |
|
33 | + } |
|
34 | 34 | } |
@@ -129,7 +129,7 @@ |
||
129 | 129 | $data = $result->fetchAll(); |
130 | 130 | $result->closeCursor(); |
131 | 131 | |
132 | - $entities = array_map(function ($row) { |
|
132 | + $entities = array_map(function($row) { |
|
133 | 133 | return PublicKeyToken::fromRow($row); |
134 | 134 | }, $data); |
135 | 135 |
@@ -36,224 +36,224 @@ |
||
36 | 36 | * @template-extends QBMapper<PublicKeyToken> |
37 | 37 | */ |
38 | 38 | class PublicKeyTokenMapper extends QBMapper { |
39 | - public function __construct(IDBConnection $db) { |
|
40 | - parent::__construct($db, 'authtoken'); |
|
41 | - } |
|
39 | + public function __construct(IDBConnection $db) { |
|
40 | + parent::__construct($db, 'authtoken'); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Invalidate (delete) a given token |
|
45 | - * |
|
46 | - * @param string $token |
|
47 | - */ |
|
48 | - public function invalidate(string $token) { |
|
49 | - /* @var $qb IQueryBuilder */ |
|
50 | - $qb = $this->db->getQueryBuilder(); |
|
51 | - $qb->delete($this->tableName) |
|
52 | - ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
53 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
54 | - ->execute(); |
|
55 | - } |
|
43 | + /** |
|
44 | + * Invalidate (delete) a given token |
|
45 | + * |
|
46 | + * @param string $token |
|
47 | + */ |
|
48 | + public function invalidate(string $token) { |
|
49 | + /* @var $qb IQueryBuilder */ |
|
50 | + $qb = $this->db->getQueryBuilder(); |
|
51 | + $qb->delete($this->tableName) |
|
52 | + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
53 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
54 | + ->execute(); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param int $olderThan |
|
59 | - * @param int $remember |
|
60 | - */ |
|
61 | - public function invalidateOld(int $olderThan, int $remember = IToken::DO_NOT_REMEMBER) { |
|
62 | - /* @var $qb IQueryBuilder */ |
|
63 | - $qb = $this->db->getQueryBuilder(); |
|
64 | - $qb->delete($this->tableName) |
|
65 | - ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
66 | - ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
|
67 | - ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
|
68 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
69 | - ->execute(); |
|
70 | - } |
|
57 | + /** |
|
58 | + * @param int $olderThan |
|
59 | + * @param int $remember |
|
60 | + */ |
|
61 | + public function invalidateOld(int $olderThan, int $remember = IToken::DO_NOT_REMEMBER) { |
|
62 | + /* @var $qb IQueryBuilder */ |
|
63 | + $qb = $this->db->getQueryBuilder(); |
|
64 | + $qb->delete($this->tableName) |
|
65 | + ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
66 | + ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
|
67 | + ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
|
68 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
69 | + ->execute(); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Get the user UID for the given token |
|
74 | - * |
|
75 | - * @throws DoesNotExistException |
|
76 | - */ |
|
77 | - public function getToken(string $token): PublicKeyToken { |
|
78 | - /* @var $qb IQueryBuilder */ |
|
79 | - $qb = $this->db->getQueryBuilder(); |
|
80 | - $result = $qb->select('*') |
|
81 | - ->from($this->tableName) |
|
82 | - ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
83 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
84 | - ->execute(); |
|
72 | + /** |
|
73 | + * Get the user UID for the given token |
|
74 | + * |
|
75 | + * @throws DoesNotExistException |
|
76 | + */ |
|
77 | + public function getToken(string $token): PublicKeyToken { |
|
78 | + /* @var $qb IQueryBuilder */ |
|
79 | + $qb = $this->db->getQueryBuilder(); |
|
80 | + $result = $qb->select('*') |
|
81 | + ->from($this->tableName) |
|
82 | + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
83 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
84 | + ->execute(); |
|
85 | 85 | |
86 | - $data = $result->fetch(); |
|
87 | - $result->closeCursor(); |
|
88 | - if ($data === false) { |
|
89 | - throw new DoesNotExistException('token does not exist'); |
|
90 | - } |
|
91 | - return PublicKeyToken::fromRow($data); |
|
92 | - } |
|
86 | + $data = $result->fetch(); |
|
87 | + $result->closeCursor(); |
|
88 | + if ($data === false) { |
|
89 | + throw new DoesNotExistException('token does not exist'); |
|
90 | + } |
|
91 | + return PublicKeyToken::fromRow($data); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Get the token for $id |
|
96 | - * |
|
97 | - * @throws DoesNotExistException |
|
98 | - */ |
|
99 | - public function getTokenById(int $id): PublicKeyToken { |
|
100 | - /* @var $qb IQueryBuilder */ |
|
101 | - $qb = $this->db->getQueryBuilder(); |
|
102 | - $result = $qb->select('*') |
|
103 | - ->from($this->tableName) |
|
104 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
105 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
106 | - ->execute(); |
|
94 | + /** |
|
95 | + * Get the token for $id |
|
96 | + * |
|
97 | + * @throws DoesNotExistException |
|
98 | + */ |
|
99 | + public function getTokenById(int $id): PublicKeyToken { |
|
100 | + /* @var $qb IQueryBuilder */ |
|
101 | + $qb = $this->db->getQueryBuilder(); |
|
102 | + $result = $qb->select('*') |
|
103 | + ->from($this->tableName) |
|
104 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
105 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
106 | + ->execute(); |
|
107 | 107 | |
108 | - $data = $result->fetch(); |
|
109 | - $result->closeCursor(); |
|
110 | - if ($data === false) { |
|
111 | - throw new DoesNotExistException('token does not exist'); |
|
112 | - } |
|
113 | - return PublicKeyToken::fromRow($data); |
|
114 | - } |
|
108 | + $data = $result->fetch(); |
|
109 | + $result->closeCursor(); |
|
110 | + if ($data === false) { |
|
111 | + throw new DoesNotExistException('token does not exist'); |
|
112 | + } |
|
113 | + return PublicKeyToken::fromRow($data); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Get all tokens of a user |
|
118 | - * |
|
119 | - * The provider may limit the number of result rows in case of an abuse |
|
120 | - * where a high number of (session) tokens is generated |
|
121 | - * |
|
122 | - * @param string $uid |
|
123 | - * @return PublicKeyToken[] |
|
124 | - */ |
|
125 | - public function getTokenByUser(string $uid): array { |
|
126 | - /* @var $qb IQueryBuilder */ |
|
127 | - $qb = $this->db->getQueryBuilder(); |
|
128 | - $qb->select('*') |
|
129 | - ->from($this->tableName) |
|
130 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
131 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
132 | - ->setMaxResults(1000); |
|
133 | - $result = $qb->execute(); |
|
134 | - $data = $result->fetchAll(); |
|
135 | - $result->closeCursor(); |
|
116 | + /** |
|
117 | + * Get all tokens of a user |
|
118 | + * |
|
119 | + * The provider may limit the number of result rows in case of an abuse |
|
120 | + * where a high number of (session) tokens is generated |
|
121 | + * |
|
122 | + * @param string $uid |
|
123 | + * @return PublicKeyToken[] |
|
124 | + */ |
|
125 | + public function getTokenByUser(string $uid): array { |
|
126 | + /* @var $qb IQueryBuilder */ |
|
127 | + $qb = $this->db->getQueryBuilder(); |
|
128 | + $qb->select('*') |
|
129 | + ->from($this->tableName) |
|
130 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
131 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
132 | + ->setMaxResults(1000); |
|
133 | + $result = $qb->execute(); |
|
134 | + $data = $result->fetchAll(); |
|
135 | + $result->closeCursor(); |
|
136 | 136 | |
137 | - $entities = array_map(function ($row) { |
|
138 | - return PublicKeyToken::fromRow($row); |
|
139 | - }, $data); |
|
137 | + $entities = array_map(function ($row) { |
|
138 | + return PublicKeyToken::fromRow($row); |
|
139 | + }, $data); |
|
140 | 140 | |
141 | - return $entities; |
|
142 | - } |
|
141 | + return $entities; |
|
142 | + } |
|
143 | 143 | |
144 | - public function deleteById(string $uid, int $id) { |
|
145 | - /* @var $qb IQueryBuilder */ |
|
146 | - $qb = $this->db->getQueryBuilder(); |
|
147 | - $qb->delete($this->tableName) |
|
148 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
149 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
150 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
151 | - $qb->execute(); |
|
152 | - } |
|
144 | + public function deleteById(string $uid, int $id) { |
|
145 | + /* @var $qb IQueryBuilder */ |
|
146 | + $qb = $this->db->getQueryBuilder(); |
|
147 | + $qb->delete($this->tableName) |
|
148 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
149 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
150 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
151 | + $qb->execute(); |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * delete all auth token which belong to a specific client if the client was deleted |
|
156 | - * |
|
157 | - * @param string $name |
|
158 | - */ |
|
159 | - public function deleteByName(string $name) { |
|
160 | - $qb = $this->db->getQueryBuilder(); |
|
161 | - $qb->delete($this->tableName) |
|
162 | - ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)) |
|
163 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
164 | - $qb->execute(); |
|
165 | - } |
|
154 | + /** |
|
155 | + * delete all auth token which belong to a specific client if the client was deleted |
|
156 | + * |
|
157 | + * @param string $name |
|
158 | + */ |
|
159 | + public function deleteByName(string $name) { |
|
160 | + $qb = $this->db->getQueryBuilder(); |
|
161 | + $qb->delete($this->tableName) |
|
162 | + ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)) |
|
163 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
164 | + $qb->execute(); |
|
165 | + } |
|
166 | 166 | |
167 | - public function deleteTempToken(PublicKeyToken $except) { |
|
168 | - $qb = $this->db->getQueryBuilder(); |
|
167 | + public function deleteTempToken(PublicKeyToken $except) { |
|
168 | + $qb = $this->db->getQueryBuilder(); |
|
169 | 169 | |
170 | - $qb->delete($this->tableName) |
|
171 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($except->getUID()))) |
|
172 | - ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN))) |
|
173 | - ->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($except->getId()))) |
|
174 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
170 | + $qb->delete($this->tableName) |
|
171 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($except->getUID()))) |
|
172 | + ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN))) |
|
173 | + ->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($except->getId()))) |
|
174 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
175 | 175 | |
176 | - $qb->execute(); |
|
177 | - } |
|
176 | + $qb->execute(); |
|
177 | + } |
|
178 | 178 | |
179 | - public function hasExpiredTokens(string $uid): bool { |
|
180 | - $qb = $this->db->getQueryBuilder(); |
|
181 | - $qb->select('*') |
|
182 | - ->from($this->tableName) |
|
183 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
184 | - ->andWhere($qb->expr()->eq('password_invalid', $qb->createNamedParameter(true), IQueryBuilder::PARAM_BOOL)) |
|
185 | - ->setMaxResults(1); |
|
179 | + public function hasExpiredTokens(string $uid): bool { |
|
180 | + $qb = $this->db->getQueryBuilder(); |
|
181 | + $qb->select('*') |
|
182 | + ->from($this->tableName) |
|
183 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
184 | + ->andWhere($qb->expr()->eq('password_invalid', $qb->createNamedParameter(true), IQueryBuilder::PARAM_BOOL)) |
|
185 | + ->setMaxResults(1); |
|
186 | 186 | |
187 | - $cursor = $qb->execute(); |
|
188 | - $data = $cursor->fetchAll(); |
|
189 | - $cursor->closeCursor(); |
|
187 | + $cursor = $qb->execute(); |
|
188 | + $data = $cursor->fetchAll(); |
|
189 | + $cursor->closeCursor(); |
|
190 | 190 | |
191 | - return count($data) === 1; |
|
192 | - } |
|
191 | + return count($data) === 1; |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Update the last activity timestamp |
|
196 | - * |
|
197 | - * In highly concurrent setups it can happen that two parallel processes |
|
198 | - * trigger the update at (nearly) the same time. In that special case it's |
|
199 | - * not necessary to hit the database with two actual updates. Therefore the |
|
200 | - * target last activity is included in the WHERE clause with a few seconds |
|
201 | - * of tolerance. |
|
202 | - * |
|
203 | - * Example: |
|
204 | - * - process 1 (P1) reads the token at timestamp 1500 |
|
205 | - * - process 1 (P2) reads the token at timestamp 1501 |
|
206 | - * - activity update interval is 100 |
|
207 | - * |
|
208 | - * This means |
|
209 | - * |
|
210 | - * - P1 will see a last_activity smaller than the current time and update |
|
211 | - * the token row |
|
212 | - * - If P2 reads after P1 had written, it will see 1600 as last activity |
|
213 | - * and the comparison on last_activity won't be truthy. This means no rows |
|
214 | - * need to be updated a second time |
|
215 | - * - If P2 reads before P1 had written, it will see 1501 as last activity, |
|
216 | - * but the comparison on last_activity will still not be truthy and the |
|
217 | - * token row is not updated a second time |
|
218 | - * |
|
219 | - * @param IToken $token |
|
220 | - * @param int $now |
|
221 | - */ |
|
222 | - public function updateActivity(IToken $token, int $now): void { |
|
223 | - $qb = $this->db->getQueryBuilder(); |
|
224 | - $update = $qb->update($this->getTableName()) |
|
225 | - ->set('last_activity', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT)) |
|
226 | - ->where( |
|
227 | - $qb->expr()->eq('id', $qb->createNamedParameter($token->getId(), IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
228 | - $qb->expr()->lt('last_activity', $qb->createNamedParameter($now - 15, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT) |
|
229 | - ); |
|
230 | - $update->executeStatement(); |
|
231 | - } |
|
194 | + /** |
|
195 | + * Update the last activity timestamp |
|
196 | + * |
|
197 | + * In highly concurrent setups it can happen that two parallel processes |
|
198 | + * trigger the update at (nearly) the same time. In that special case it's |
|
199 | + * not necessary to hit the database with two actual updates. Therefore the |
|
200 | + * target last activity is included in the WHERE clause with a few seconds |
|
201 | + * of tolerance. |
|
202 | + * |
|
203 | + * Example: |
|
204 | + * - process 1 (P1) reads the token at timestamp 1500 |
|
205 | + * - process 1 (P2) reads the token at timestamp 1501 |
|
206 | + * - activity update interval is 100 |
|
207 | + * |
|
208 | + * This means |
|
209 | + * |
|
210 | + * - P1 will see a last_activity smaller than the current time and update |
|
211 | + * the token row |
|
212 | + * - If P2 reads after P1 had written, it will see 1600 as last activity |
|
213 | + * and the comparison on last_activity won't be truthy. This means no rows |
|
214 | + * need to be updated a second time |
|
215 | + * - If P2 reads before P1 had written, it will see 1501 as last activity, |
|
216 | + * but the comparison on last_activity will still not be truthy and the |
|
217 | + * token row is not updated a second time |
|
218 | + * |
|
219 | + * @param IToken $token |
|
220 | + * @param int $now |
|
221 | + */ |
|
222 | + public function updateActivity(IToken $token, int $now): void { |
|
223 | + $qb = $this->db->getQueryBuilder(); |
|
224 | + $update = $qb->update($this->getTableName()) |
|
225 | + ->set('last_activity', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT)) |
|
226 | + ->where( |
|
227 | + $qb->expr()->eq('id', $qb->createNamedParameter($token->getId(), IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
228 | + $qb->expr()->lt('last_activity', $qb->createNamedParameter($now - 15, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT) |
|
229 | + ); |
|
230 | + $update->executeStatement(); |
|
231 | + } |
|
232 | 232 | |
233 | - public function updateHashesForUser(string $userId, string $passwordHash): void { |
|
234 | - $qb = $this->db->getQueryBuilder(); |
|
235 | - $update = $qb->update($this->getTableName()) |
|
236 | - ->set('password_hash', $qb->createNamedParameter($passwordHash)) |
|
237 | - ->where( |
|
238 | - $qb->expr()->eq('uid', $qb->createNamedParameter($userId)) |
|
239 | - ); |
|
240 | - $update->executeStatement(); |
|
241 | - } |
|
233 | + public function updateHashesForUser(string $userId, string $passwordHash): void { |
|
234 | + $qb = $this->db->getQueryBuilder(); |
|
235 | + $update = $qb->update($this->getTableName()) |
|
236 | + ->set('password_hash', $qb->createNamedParameter($passwordHash)) |
|
237 | + ->where( |
|
238 | + $qb->expr()->eq('uid', $qb->createNamedParameter($userId)) |
|
239 | + ); |
|
240 | + $update->executeStatement(); |
|
241 | + } |
|
242 | 242 | |
243 | - public function getFirstTokenForUser(string $userId): ?PublicKeyToken { |
|
244 | - $qb = $this->db->getQueryBuilder(); |
|
245 | - $qb->select('*') |
|
246 | - ->from($this->getTableName()) |
|
247 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($userId))) |
|
248 | - ->setMaxResults(1) |
|
249 | - ->orderBy('id'); |
|
250 | - $result = $qb->executeQuery(); |
|
243 | + public function getFirstTokenForUser(string $userId): ?PublicKeyToken { |
|
244 | + $qb = $this->db->getQueryBuilder(); |
|
245 | + $qb->select('*') |
|
246 | + ->from($this->getTableName()) |
|
247 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($userId))) |
|
248 | + ->setMaxResults(1) |
|
249 | + ->orderBy('id'); |
|
250 | + $result = $qb->executeQuery(); |
|
251 | 251 | |
252 | - $data = $result->fetch(); |
|
253 | - $result->closeCursor(); |
|
254 | - if ($data === false) { |
|
255 | - return null; |
|
256 | - } |
|
257 | - return PublicKeyToken::fromRow($data); |
|
258 | - } |
|
252 | + $data = $result->fetch(); |
|
253 | + $result->closeCursor(); |
|
254 | + if ($data === false) { |
|
255 | + return null; |
|
256 | + } |
|
257 | + return PublicKeyToken::fromRow($data); |
|
258 | + } |
|
259 | 259 | } |
@@ -36,7 +36,7 @@ |
||
36 | 36 | $l = \OC::$server->getL10N('federation'); |
37 | 37 | $this->parameterList = $missingParameters; |
38 | 38 | $parameterList = implode(',', $missingParameters); |
39 | - $message = 'Parameters missing in order to complete the request. Missing Parameters: ' . $parameterList; |
|
39 | + $message = 'Parameters missing in order to complete the request. Missing Parameters: '.$parameterList; |
|
40 | 40 | $hint = $l->t('Parameters missing in order to complete the request. Missing Parameters: "%s"', [$parameterList]); |
41 | 41 | parent::__construct($message, $hint); |
42 | 42 | } |
@@ -32,48 +32,48 @@ |
||
32 | 32 | * @since 14.0.0 |
33 | 33 | */ |
34 | 34 | class BadRequestException extends HintException { |
35 | - /** |
|
36 | - * @var string[] $parameterList |
|
37 | - */ |
|
38 | - private $parameterList; |
|
35 | + /** |
|
36 | + * @var string[] $parameterList |
|
37 | + */ |
|
38 | + private $parameterList; |
|
39 | 39 | |
40 | - /** |
|
41 | - * BadRequestException constructor. |
|
42 | - * |
|
43 | - * @since 14.0.0 |
|
44 | - * |
|
45 | - * @param array $missingParameters |
|
46 | - */ |
|
47 | - public function __construct(array $missingParameters) { |
|
48 | - $l = \OC::$server->getL10N('federation'); |
|
49 | - $this->parameterList = $missingParameters; |
|
50 | - $parameterList = implode(',', $missingParameters); |
|
51 | - $message = 'Parameters missing in order to complete the request. Missing Parameters: ' . $parameterList; |
|
52 | - $hint = $l->t('Parameters missing in order to complete the request. Missing Parameters: "%s"', [$parameterList]); |
|
53 | - parent::__construct($message, $hint); |
|
54 | - } |
|
40 | + /** |
|
41 | + * BadRequestException constructor. |
|
42 | + * |
|
43 | + * @since 14.0.0 |
|
44 | + * |
|
45 | + * @param array $missingParameters |
|
46 | + */ |
|
47 | + public function __construct(array $missingParameters) { |
|
48 | + $l = \OC::$server->getL10N('federation'); |
|
49 | + $this->parameterList = $missingParameters; |
|
50 | + $parameterList = implode(',', $missingParameters); |
|
51 | + $message = 'Parameters missing in order to complete the request. Missing Parameters: ' . $parameterList; |
|
52 | + $hint = $l->t('Parameters missing in order to complete the request. Missing Parameters: "%s"', [$parameterList]); |
|
53 | + parent::__construct($message, $hint); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * get array with the return message as defined in the OCM API |
|
58 | - * |
|
59 | - * @since 14.0.0 |
|
60 | - * |
|
61 | - * @return array{message: string, validationErrors: array{message: string, name: string}[]} |
|
62 | - */ |
|
63 | - public function getReturnMessage() { |
|
64 | - $result = [ |
|
65 | - 'message' => 'RESOURCE_NOT_FOUND', |
|
66 | - 'validationErrors' => [ |
|
67 | - ] |
|
68 | - ]; |
|
56 | + /** |
|
57 | + * get array with the return message as defined in the OCM API |
|
58 | + * |
|
59 | + * @since 14.0.0 |
|
60 | + * |
|
61 | + * @return array{message: string, validationErrors: array{message: string, name: string}[]} |
|
62 | + */ |
|
63 | + public function getReturnMessage() { |
|
64 | + $result = [ |
|
65 | + 'message' => 'RESOURCE_NOT_FOUND', |
|
66 | + 'validationErrors' => [ |
|
67 | + ] |
|
68 | + ]; |
|
69 | 69 | |
70 | - foreach ($this->parameterList as $missingParameter) { |
|
71 | - $result['validationErrors'][] = [ |
|
72 | - 'name' => $missingParameter, |
|
73 | - 'message' => 'NOT_FOUND' |
|
74 | - ]; |
|
75 | - } |
|
70 | + foreach ($this->parameterList as $missingParameter) { |
|
71 | + $result['validationErrors'][] = [ |
|
72 | + 'name' => $missingParameter, |
|
73 | + 'message' => 'NOT_FOUND' |
|
74 | + ]; |
|
75 | + } |
|
76 | 76 | |
77 | - return $result; |
|
78 | - } |
|
77 | + return $result; |
|
78 | + } |
|
79 | 79 | } |
@@ -59,9 +59,9 @@ discard block |
||
59 | 59 | private function getBlobClient() { |
60 | 60 | if (!$this->blobClient) { |
61 | 61 | $protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https'; |
62 | - $connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey; |
|
62 | + $connectionString = "DefaultEndpointsProtocol=".$protocol.";AccountName=".$this->accountName.";AccountKey=".$this->accountKey; |
|
63 | 63 | if ($this->endpoint) { |
64 | - $connectionString .= ';BlobEndpoint=' . $this->endpoint; |
|
64 | + $connectionString .= ';BlobEndpoint='.$this->endpoint; |
|
65 | 65 | } |
66 | 66 | $this->blobClient = BlobRestProxy::createBlobService($connectionString); |
67 | 67 | |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * @return string the container or bucket name where objects are stored |
77 | 77 | */ |
78 | 78 | public function getStorageId() { |
79 | - return 'azure::blob::' . $this->containerName; |
|
79 | + return 'azure::blob::'.$this->containerName; |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -29,109 +29,109 @@ |
||
29 | 29 | use OCP\Files\ObjectStore\IObjectStore; |
30 | 30 | |
31 | 31 | class Azure implements IObjectStore { |
32 | - /** @var string */ |
|
33 | - private $containerName; |
|
34 | - /** @var string */ |
|
35 | - private $accountName; |
|
36 | - /** @var string */ |
|
37 | - private $accountKey; |
|
38 | - /** @var BlobRestProxy|null */ |
|
39 | - private $blobClient = null; |
|
40 | - /** @var string|null */ |
|
41 | - private $endpoint = null; |
|
42 | - /** @var bool */ |
|
43 | - private $autoCreate = false; |
|
32 | + /** @var string */ |
|
33 | + private $containerName; |
|
34 | + /** @var string */ |
|
35 | + private $accountName; |
|
36 | + /** @var string */ |
|
37 | + private $accountKey; |
|
38 | + /** @var BlobRestProxy|null */ |
|
39 | + private $blobClient = null; |
|
40 | + /** @var string|null */ |
|
41 | + private $endpoint = null; |
|
42 | + /** @var bool */ |
|
43 | + private $autoCreate = false; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param array $parameters |
|
47 | - */ |
|
48 | - public function __construct($parameters) { |
|
49 | - $this->containerName = $parameters['container']; |
|
50 | - $this->accountName = $parameters['account_name']; |
|
51 | - $this->accountKey = $parameters['account_key']; |
|
52 | - if (isset($parameters['endpoint'])) { |
|
53 | - $this->endpoint = $parameters['endpoint']; |
|
54 | - } |
|
55 | - if (isset($parameters['autocreate'])) { |
|
56 | - $this->autoCreate = $parameters['autocreate']; |
|
57 | - } |
|
58 | - } |
|
45 | + /** |
|
46 | + * @param array $parameters |
|
47 | + */ |
|
48 | + public function __construct($parameters) { |
|
49 | + $this->containerName = $parameters['container']; |
|
50 | + $this->accountName = $parameters['account_name']; |
|
51 | + $this->accountKey = $parameters['account_key']; |
|
52 | + if (isset($parameters['endpoint'])) { |
|
53 | + $this->endpoint = $parameters['endpoint']; |
|
54 | + } |
|
55 | + if (isset($parameters['autocreate'])) { |
|
56 | + $this->autoCreate = $parameters['autocreate']; |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @return BlobRestProxy |
|
62 | - */ |
|
63 | - private function getBlobClient() { |
|
64 | - if (!$this->blobClient) { |
|
65 | - $protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https'; |
|
66 | - $connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey; |
|
67 | - if ($this->endpoint) { |
|
68 | - $connectionString .= ';BlobEndpoint=' . $this->endpoint; |
|
69 | - } |
|
70 | - $this->blobClient = BlobRestProxy::createBlobService($connectionString); |
|
60 | + /** |
|
61 | + * @return BlobRestProxy |
|
62 | + */ |
|
63 | + private function getBlobClient() { |
|
64 | + if (!$this->blobClient) { |
|
65 | + $protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https'; |
|
66 | + $connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey; |
|
67 | + if ($this->endpoint) { |
|
68 | + $connectionString .= ';BlobEndpoint=' . $this->endpoint; |
|
69 | + } |
|
70 | + $this->blobClient = BlobRestProxy::createBlobService($connectionString); |
|
71 | 71 | |
72 | - if ($this->autoCreate) { |
|
73 | - try { |
|
74 | - $this->blobClient->createContainer($this->containerName); |
|
75 | - } catch (ServiceException $e) { |
|
76 | - if ($e->getCode() === 409) { |
|
77 | - // already exists |
|
78 | - } else { |
|
79 | - throw $e; |
|
80 | - } |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
84 | - return $this->blobClient; |
|
85 | - } |
|
72 | + if ($this->autoCreate) { |
|
73 | + try { |
|
74 | + $this->blobClient->createContainer($this->containerName); |
|
75 | + } catch (ServiceException $e) { |
|
76 | + if ($e->getCode() === 409) { |
|
77 | + // already exists |
|
78 | + } else { |
|
79 | + throw $e; |
|
80 | + } |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + return $this->blobClient; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @return string the container or bucket name where objects are stored |
|
89 | - */ |
|
90 | - public function getStorageId() { |
|
91 | - return 'azure::blob::' . $this->containerName; |
|
92 | - } |
|
87 | + /** |
|
88 | + * @return string the container or bucket name where objects are stored |
|
89 | + */ |
|
90 | + public function getStorageId() { |
|
91 | + return 'azure::blob::' . $this->containerName; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @param string $urn the unified resource name used to identify the object |
|
96 | - * @return resource stream with the read data |
|
97 | - * @throws \Exception when something goes wrong, message will be logged |
|
98 | - */ |
|
99 | - public function readObject($urn) { |
|
100 | - $blob = $this->getBlobClient()->getBlob($this->containerName, $urn); |
|
101 | - return $blob->getContentStream(); |
|
102 | - } |
|
94 | + /** |
|
95 | + * @param string $urn the unified resource name used to identify the object |
|
96 | + * @return resource stream with the read data |
|
97 | + * @throws \Exception when something goes wrong, message will be logged |
|
98 | + */ |
|
99 | + public function readObject($urn) { |
|
100 | + $blob = $this->getBlobClient()->getBlob($this->containerName, $urn); |
|
101 | + return $blob->getContentStream(); |
|
102 | + } |
|
103 | 103 | |
104 | - public function writeObject($urn, $stream, string $mimetype = null) { |
|
105 | - $options = new CreateBlockBlobOptions(); |
|
106 | - if ($mimetype) { |
|
107 | - $options->setContentType($mimetype); |
|
108 | - } |
|
109 | - $this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream, $options); |
|
110 | - } |
|
104 | + public function writeObject($urn, $stream, string $mimetype = null) { |
|
105 | + $options = new CreateBlockBlobOptions(); |
|
106 | + if ($mimetype) { |
|
107 | + $options->setContentType($mimetype); |
|
108 | + } |
|
109 | + $this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream, $options); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * @param string $urn the unified resource name used to identify the object |
|
114 | - * @return void |
|
115 | - * @throws \Exception when something goes wrong, message will be logged |
|
116 | - */ |
|
117 | - public function deleteObject($urn) { |
|
118 | - $this->getBlobClient()->deleteBlob($this->containerName, $urn); |
|
119 | - } |
|
112 | + /** |
|
113 | + * @param string $urn the unified resource name used to identify the object |
|
114 | + * @return void |
|
115 | + * @throws \Exception when something goes wrong, message will be logged |
|
116 | + */ |
|
117 | + public function deleteObject($urn) { |
|
118 | + $this->getBlobClient()->deleteBlob($this->containerName, $urn); |
|
119 | + } |
|
120 | 120 | |
121 | - public function objectExists($urn) { |
|
122 | - try { |
|
123 | - $this->getBlobClient()->getBlobMetadata($this->containerName, $urn); |
|
124 | - return true; |
|
125 | - } catch (ServiceException $e) { |
|
126 | - if ($e->getCode() === 404) { |
|
127 | - return false; |
|
128 | - } else { |
|
129 | - throw $e; |
|
130 | - } |
|
131 | - } |
|
132 | - } |
|
121 | + public function objectExists($urn) { |
|
122 | + try { |
|
123 | + $this->getBlobClient()->getBlobMetadata($this->containerName, $urn); |
|
124 | + return true; |
|
125 | + } catch (ServiceException $e) { |
|
126 | + if ($e->getCode() === 404) { |
|
127 | + return false; |
|
128 | + } else { |
|
129 | + throw $e; |
|
130 | + } |
|
131 | + } |
|
132 | + } |
|
133 | 133 | |
134 | - public function copyObject($from, $to) { |
|
135 | - $this->getBlobClient()->copyBlob($this->containerName, $to, $this->containerName, $from); |
|
136 | - } |
|
134 | + public function copyObject($from, $to) { |
|
135 | + $this->getBlobClient()->copyBlob($this->containerName, $to, $this->containerName, $from); |
|
136 | + } |
|
137 | 137 | } |
@@ -184,7 +184,7 @@ |
||
184 | 184 | if (is_array($scope)) { |
185 | 185 | parent::setScope(json_encode($scope)); |
186 | 186 | } else { |
187 | - parent::setScope((string)$scope); |
|
187 | + parent::setScope((string) $scope); |
|
188 | 188 | } |
189 | 189 | } |
190 | 190 |
@@ -49,192 +49,192 @@ |
||
49 | 49 | * @method setPasswordHash(string $hash) |
50 | 50 | */ |
51 | 51 | class PublicKeyToken extends Entity implements INamedToken, IWipeableToken { |
52 | - public const VERSION = 2; |
|
53 | - |
|
54 | - /** @var string user UID */ |
|
55 | - protected $uid; |
|
56 | - |
|
57 | - /** @var string login name used for generating the token */ |
|
58 | - protected $loginName; |
|
59 | - |
|
60 | - /** @var string encrypted user password */ |
|
61 | - protected $password; |
|
62 | - |
|
63 | - /** @var string hashed user password */ |
|
64 | - protected $passwordHash; |
|
65 | - |
|
66 | - /** @var string token name (e.g. browser/OS) */ |
|
67 | - protected $name; |
|
68 | - |
|
69 | - /** @var string */ |
|
70 | - protected $token; |
|
71 | - |
|
72 | - /** @var int */ |
|
73 | - protected $type; |
|
74 | - |
|
75 | - /** @var int */ |
|
76 | - protected $remember; |
|
77 | - |
|
78 | - /** @var int */ |
|
79 | - protected $lastActivity; |
|
80 | - |
|
81 | - /** @var int */ |
|
82 | - protected $lastCheck; |
|
83 | - |
|
84 | - /** @var string */ |
|
85 | - protected $scope; |
|
86 | - |
|
87 | - /** @var int */ |
|
88 | - protected $expires; |
|
89 | - |
|
90 | - /** @var string */ |
|
91 | - protected $privateKey; |
|
92 | - |
|
93 | - /** @var string */ |
|
94 | - protected $publicKey; |
|
95 | - |
|
96 | - /** @var int */ |
|
97 | - protected $version; |
|
98 | - |
|
99 | - /** @var bool */ |
|
100 | - protected $passwordInvalid; |
|
101 | - |
|
102 | - public function __construct() { |
|
103 | - $this->addType('uid', 'string'); |
|
104 | - $this->addType('loginName', 'string'); |
|
105 | - $this->addType('password', 'string'); |
|
106 | - $this->addType('passwordHash', 'string'); |
|
107 | - $this->addType('name', 'string'); |
|
108 | - $this->addType('token', 'string'); |
|
109 | - $this->addType('type', 'int'); |
|
110 | - $this->addType('remember', 'int'); |
|
111 | - $this->addType('lastActivity', 'int'); |
|
112 | - $this->addType('lastCheck', 'int'); |
|
113 | - $this->addType('scope', 'string'); |
|
114 | - $this->addType('expires', 'int'); |
|
115 | - $this->addType('publicKey', 'string'); |
|
116 | - $this->addType('privateKey', 'string'); |
|
117 | - $this->addType('version', 'int'); |
|
118 | - $this->addType('passwordInvalid', 'bool'); |
|
119 | - } |
|
120 | - |
|
121 | - public function getId(): int { |
|
122 | - return $this->id; |
|
123 | - } |
|
124 | - |
|
125 | - public function getUID(): string { |
|
126 | - return $this->uid; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Get the login name used when generating the token |
|
131 | - * |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - public function getLoginName(): string { |
|
135 | - return parent::getLoginName(); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Get the (encrypted) login password |
|
140 | - * |
|
141 | - * @return string|null |
|
142 | - */ |
|
143 | - public function getPassword() { |
|
144 | - return parent::getPassword(); |
|
145 | - } |
|
146 | - |
|
147 | - public function jsonSerialize(): array { |
|
148 | - return [ |
|
149 | - 'id' => $this->id, |
|
150 | - 'name' => $this->name, |
|
151 | - 'lastActivity' => $this->lastActivity, |
|
152 | - 'type' => $this->type, |
|
153 | - 'scope' => $this->getScopeAsArray() |
|
154 | - ]; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get the timestamp of the last password check |
|
159 | - * |
|
160 | - * @return int |
|
161 | - */ |
|
162 | - public function getLastCheck(): int { |
|
163 | - return parent::getLastCheck(); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Get the timestamp of the last password check |
|
168 | - * |
|
169 | - * @param int $time |
|
170 | - */ |
|
171 | - public function setLastCheck(int $time) { |
|
172 | - parent::setLastCheck($time); |
|
173 | - } |
|
174 | - |
|
175 | - public function getScope(): string { |
|
176 | - $scope = parent::getScope(); |
|
177 | - if ($scope === null) { |
|
178 | - return ''; |
|
179 | - } |
|
180 | - |
|
181 | - return $scope; |
|
182 | - } |
|
183 | - |
|
184 | - public function getScopeAsArray(): array { |
|
185 | - $scope = json_decode($this->getScope(), true); |
|
186 | - if (!$scope) { |
|
187 | - return [ |
|
188 | - 'filesystem' => true |
|
189 | - ]; |
|
190 | - } |
|
191 | - return $scope; |
|
192 | - } |
|
193 | - |
|
194 | - public function setScope($scope) { |
|
195 | - if (is_array($scope)) { |
|
196 | - parent::setScope(json_encode($scope)); |
|
197 | - } else { |
|
198 | - parent::setScope((string)$scope); |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - public function getName(): string { |
|
203 | - return parent::getName(); |
|
204 | - } |
|
205 | - |
|
206 | - public function setName(string $name): void { |
|
207 | - parent::setName($name); |
|
208 | - } |
|
209 | - |
|
210 | - public function getRemember(): int { |
|
211 | - return parent::getRemember(); |
|
212 | - } |
|
213 | - |
|
214 | - public function setToken(string $token) { |
|
215 | - parent::setToken($token); |
|
216 | - } |
|
217 | - |
|
218 | - public function setPassword(string $password = null) { |
|
219 | - parent::setPassword($password); |
|
220 | - } |
|
221 | - |
|
222 | - public function setExpires($expires) { |
|
223 | - parent::setExpires($expires); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @return int|null |
|
228 | - */ |
|
229 | - public function getExpires() { |
|
230 | - return parent::getExpires(); |
|
231 | - } |
|
232 | - |
|
233 | - public function setPasswordInvalid(bool $invalid) { |
|
234 | - parent::setPasswordInvalid($invalid); |
|
235 | - } |
|
236 | - |
|
237 | - public function wipe(): void { |
|
238 | - parent::setType(IToken::WIPE_TOKEN); |
|
239 | - } |
|
52 | + public const VERSION = 2; |
|
53 | + |
|
54 | + /** @var string user UID */ |
|
55 | + protected $uid; |
|
56 | + |
|
57 | + /** @var string login name used for generating the token */ |
|
58 | + protected $loginName; |
|
59 | + |
|
60 | + /** @var string encrypted user password */ |
|
61 | + protected $password; |
|
62 | + |
|
63 | + /** @var string hashed user password */ |
|
64 | + protected $passwordHash; |
|
65 | + |
|
66 | + /** @var string token name (e.g. browser/OS) */ |
|
67 | + protected $name; |
|
68 | + |
|
69 | + /** @var string */ |
|
70 | + protected $token; |
|
71 | + |
|
72 | + /** @var int */ |
|
73 | + protected $type; |
|
74 | + |
|
75 | + /** @var int */ |
|
76 | + protected $remember; |
|
77 | + |
|
78 | + /** @var int */ |
|
79 | + protected $lastActivity; |
|
80 | + |
|
81 | + /** @var int */ |
|
82 | + protected $lastCheck; |
|
83 | + |
|
84 | + /** @var string */ |
|
85 | + protected $scope; |
|
86 | + |
|
87 | + /** @var int */ |
|
88 | + protected $expires; |
|
89 | + |
|
90 | + /** @var string */ |
|
91 | + protected $privateKey; |
|
92 | + |
|
93 | + /** @var string */ |
|
94 | + protected $publicKey; |
|
95 | + |
|
96 | + /** @var int */ |
|
97 | + protected $version; |
|
98 | + |
|
99 | + /** @var bool */ |
|
100 | + protected $passwordInvalid; |
|
101 | + |
|
102 | + public function __construct() { |
|
103 | + $this->addType('uid', 'string'); |
|
104 | + $this->addType('loginName', 'string'); |
|
105 | + $this->addType('password', 'string'); |
|
106 | + $this->addType('passwordHash', 'string'); |
|
107 | + $this->addType('name', 'string'); |
|
108 | + $this->addType('token', 'string'); |
|
109 | + $this->addType('type', 'int'); |
|
110 | + $this->addType('remember', 'int'); |
|
111 | + $this->addType('lastActivity', 'int'); |
|
112 | + $this->addType('lastCheck', 'int'); |
|
113 | + $this->addType('scope', 'string'); |
|
114 | + $this->addType('expires', 'int'); |
|
115 | + $this->addType('publicKey', 'string'); |
|
116 | + $this->addType('privateKey', 'string'); |
|
117 | + $this->addType('version', 'int'); |
|
118 | + $this->addType('passwordInvalid', 'bool'); |
|
119 | + } |
|
120 | + |
|
121 | + public function getId(): int { |
|
122 | + return $this->id; |
|
123 | + } |
|
124 | + |
|
125 | + public function getUID(): string { |
|
126 | + return $this->uid; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Get the login name used when generating the token |
|
131 | + * |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + public function getLoginName(): string { |
|
135 | + return parent::getLoginName(); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Get the (encrypted) login password |
|
140 | + * |
|
141 | + * @return string|null |
|
142 | + */ |
|
143 | + public function getPassword() { |
|
144 | + return parent::getPassword(); |
|
145 | + } |
|
146 | + |
|
147 | + public function jsonSerialize(): array { |
|
148 | + return [ |
|
149 | + 'id' => $this->id, |
|
150 | + 'name' => $this->name, |
|
151 | + 'lastActivity' => $this->lastActivity, |
|
152 | + 'type' => $this->type, |
|
153 | + 'scope' => $this->getScopeAsArray() |
|
154 | + ]; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get the timestamp of the last password check |
|
159 | + * |
|
160 | + * @return int |
|
161 | + */ |
|
162 | + public function getLastCheck(): int { |
|
163 | + return parent::getLastCheck(); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Get the timestamp of the last password check |
|
168 | + * |
|
169 | + * @param int $time |
|
170 | + */ |
|
171 | + public function setLastCheck(int $time) { |
|
172 | + parent::setLastCheck($time); |
|
173 | + } |
|
174 | + |
|
175 | + public function getScope(): string { |
|
176 | + $scope = parent::getScope(); |
|
177 | + if ($scope === null) { |
|
178 | + return ''; |
|
179 | + } |
|
180 | + |
|
181 | + return $scope; |
|
182 | + } |
|
183 | + |
|
184 | + public function getScopeAsArray(): array { |
|
185 | + $scope = json_decode($this->getScope(), true); |
|
186 | + if (!$scope) { |
|
187 | + return [ |
|
188 | + 'filesystem' => true |
|
189 | + ]; |
|
190 | + } |
|
191 | + return $scope; |
|
192 | + } |
|
193 | + |
|
194 | + public function setScope($scope) { |
|
195 | + if (is_array($scope)) { |
|
196 | + parent::setScope(json_encode($scope)); |
|
197 | + } else { |
|
198 | + parent::setScope((string)$scope); |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + public function getName(): string { |
|
203 | + return parent::getName(); |
|
204 | + } |
|
205 | + |
|
206 | + public function setName(string $name): void { |
|
207 | + parent::setName($name); |
|
208 | + } |
|
209 | + |
|
210 | + public function getRemember(): int { |
|
211 | + return parent::getRemember(); |
|
212 | + } |
|
213 | + |
|
214 | + public function setToken(string $token) { |
|
215 | + parent::setToken($token); |
|
216 | + } |
|
217 | + |
|
218 | + public function setPassword(string $password = null) { |
|
219 | + parent::setPassword($password); |
|
220 | + } |
|
221 | + |
|
222 | + public function setExpires($expires) { |
|
223 | + parent::setExpires($expires); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @return int|null |
|
228 | + */ |
|
229 | + public function getExpires() { |
|
230 | + return parent::getExpires(); |
|
231 | + } |
|
232 | + |
|
233 | + public function setPasswordInvalid(bool $invalid) { |
|
234 | + parent::setPasswordInvalid($invalid); |
|
235 | + } |
|
236 | + |
|
237 | + public function wipe(): void { |
|
238 | + parent::setType(IToken::WIPE_TOKEN); |
|
239 | + } |
|
240 | 240 | } |
@@ -107,7 +107,7 @@ |
||
107 | 107 | * @return string language |
108 | 108 | * @since 7.0.0 |
109 | 109 | */ |
110 | - public function getLanguageCode(): string ; |
|
110 | + public function getLanguageCode(): string; |
|
111 | 111 | |
112 | 112 | /** |
113 | 113 | * * The code (en_US, fr_CA, ...) of the locale that is used for this IL10N object |
@@ -43,77 +43,77 @@ |
||
43 | 43 | * @since 6.0.0 |
44 | 44 | */ |
45 | 45 | interface IL10N { |
46 | - /** |
|
47 | - * Translating |
|
48 | - * @param string $text The text we need a translation for |
|
49 | - * @param array|string $parameters default:array() Parameters for sprintf |
|
50 | - * @return string Translation or the same text |
|
51 | - * |
|
52 | - * Returns the translation. If no translation is found, $text will be |
|
53 | - * returned. |
|
54 | - * @since 6.0.0 |
|
55 | - */ |
|
56 | - public function t(string $text, $parameters = []): string; |
|
46 | + /** |
|
47 | + * Translating |
|
48 | + * @param string $text The text we need a translation for |
|
49 | + * @param array|string $parameters default:array() Parameters for sprintf |
|
50 | + * @return string Translation or the same text |
|
51 | + * |
|
52 | + * Returns the translation. If no translation is found, $text will be |
|
53 | + * returned. |
|
54 | + * @since 6.0.0 |
|
55 | + */ |
|
56 | + public function t(string $text, $parameters = []): string; |
|
57 | 57 | |
58 | - /** |
|
59 | - * Translating |
|
60 | - * @param string $text_singular the string to translate for exactly one object |
|
61 | - * @param string $text_plural the string to translate for n objects |
|
62 | - * @param integer $count Number of objects |
|
63 | - * @param array $parameters default:array() Parameters for sprintf |
|
64 | - * @return string Translation or the same text |
|
65 | - * |
|
66 | - * Returns the translation. If no translation is found, $text will be |
|
67 | - * returned. %n will be replaced with the number of objects. |
|
68 | - * |
|
69 | - * The correct plural is determined by the plural_forms-function |
|
70 | - * provided by the po file. |
|
71 | - * @since 6.0.0 |
|
72 | - * |
|
73 | - */ |
|
74 | - public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string; |
|
58 | + /** |
|
59 | + * Translating |
|
60 | + * @param string $text_singular the string to translate for exactly one object |
|
61 | + * @param string $text_plural the string to translate for n objects |
|
62 | + * @param integer $count Number of objects |
|
63 | + * @param array $parameters default:array() Parameters for sprintf |
|
64 | + * @return string Translation or the same text |
|
65 | + * |
|
66 | + * Returns the translation. If no translation is found, $text will be |
|
67 | + * returned. %n will be replaced with the number of objects. |
|
68 | + * |
|
69 | + * The correct plural is determined by the plural_forms-function |
|
70 | + * provided by the po file. |
|
71 | + * @since 6.0.0 |
|
72 | + * |
|
73 | + */ |
|
74 | + public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string; |
|
75 | 75 | |
76 | - /** |
|
77 | - * Localization |
|
78 | - * @param string $type Type of localization |
|
79 | - * @param \DateTime|int|string $data parameters for this localization |
|
80 | - * @param array $options currently supports following options: |
|
81 | - * - 'width': handed into \Punic\Calendar::formatDate as second parameter |
|
82 | - * @return string|int|false |
|
83 | - * |
|
84 | - * Returns the localized data. |
|
85 | - * |
|
86 | - * Implemented types: |
|
87 | - * - date |
|
88 | - * - Creates a date |
|
89 | - * - l10n-field: date |
|
90 | - * - params: timestamp (int/string) |
|
91 | - * - datetime |
|
92 | - * - Creates date and time |
|
93 | - * - l10n-field: datetime |
|
94 | - * - params: timestamp (int/string) |
|
95 | - * - time |
|
96 | - * - Creates a time |
|
97 | - * - l10n-field: time |
|
98 | - * - params: timestamp (int/string) |
|
99 | - * @since 6.0.0 - parameter $options was added in 8.0.0 |
|
100 | - */ |
|
101 | - public function l(string $type, $data, array $options = []); |
|
76 | + /** |
|
77 | + * Localization |
|
78 | + * @param string $type Type of localization |
|
79 | + * @param \DateTime|int|string $data parameters for this localization |
|
80 | + * @param array $options currently supports following options: |
|
81 | + * - 'width': handed into \Punic\Calendar::formatDate as second parameter |
|
82 | + * @return string|int|false |
|
83 | + * |
|
84 | + * Returns the localized data. |
|
85 | + * |
|
86 | + * Implemented types: |
|
87 | + * - date |
|
88 | + * - Creates a date |
|
89 | + * - l10n-field: date |
|
90 | + * - params: timestamp (int/string) |
|
91 | + * - datetime |
|
92 | + * - Creates date and time |
|
93 | + * - l10n-field: datetime |
|
94 | + * - params: timestamp (int/string) |
|
95 | + * - time |
|
96 | + * - Creates a time |
|
97 | + * - l10n-field: time |
|
98 | + * - params: timestamp (int/string) |
|
99 | + * @since 6.0.0 - parameter $options was added in 8.0.0 |
|
100 | + */ |
|
101 | + public function l(string $type, $data, array $options = []); |
|
102 | 102 | |
103 | 103 | |
104 | - /** |
|
105 | - * The code (en, de, ...) of the language that is used for this IL10N object |
|
106 | - * |
|
107 | - * @return string language |
|
108 | - * @since 7.0.0 |
|
109 | - */ |
|
110 | - public function getLanguageCode(): string ; |
|
104 | + /** |
|
105 | + * The code (en, de, ...) of the language that is used for this IL10N object |
|
106 | + * |
|
107 | + * @return string language |
|
108 | + * @since 7.0.0 |
|
109 | + */ |
|
110 | + public function getLanguageCode(): string ; |
|
111 | 111 | |
112 | - /** |
|
113 | - * * The code (en_US, fr_CA, ...) of the locale that is used for this IL10N object |
|
114 | - * |
|
115 | - * @return string locale |
|
116 | - * @since 14.0.0 |
|
117 | - */ |
|
118 | - public function getLocaleCode(): string; |
|
112 | + /** |
|
113 | + * * The code (en_US, fr_CA, ...) of the locale that is used for this IL10N object |
|
114 | + * |
|
115 | + * @return string locale |
|
116 | + * @since 14.0.0 |
|
117 | + */ |
|
118 | + public function getLocaleCode(): string; |
|
119 | 119 | } |
@@ -73,19 +73,19 @@ |
||
73 | 73 | default: |
74 | 74 | foreach ($items as $key => $item) { |
75 | 75 | if (is_array($item)) { |
76 | - $output->writeln($prefix . $key . ':'); |
|
77 | - $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix); |
|
76 | + $output->writeln($prefix.$key.':'); |
|
77 | + $this->writeArrayInOutputFormat($input, $output, $item, ' '.$prefix); |
|
78 | 78 | continue; |
79 | 79 | } |
80 | 80 | if (!is_int($key) || ListCommand::class === get_class($this)) { |
81 | 81 | $value = $this->valueToString($item); |
82 | 82 | if (!is_null($value)) { |
83 | - $output->writeln($prefix . $key . ': ' . $value); |
|
83 | + $output->writeln($prefix.$key.': '.$value); |
|
84 | 84 | } else { |
85 | - $output->writeln($prefix . $key); |
|
85 | + $output->writeln($prefix.$key); |
|
86 | 86 | } |
87 | 87 | } else { |
88 | - $output->writeln($prefix . $this->valueToString($item)); |
|
88 | + $output->writeln($prefix.$this->valueToString($item)); |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | break; |
@@ -35,166 +35,166 @@ |
||
35 | 35 | use Symfony\Component\Console\Output\OutputInterface; |
36 | 36 | |
37 | 37 | class Base extends Command implements CompletionAwareInterface { |
38 | - public const OUTPUT_FORMAT_PLAIN = 'plain'; |
|
39 | - public const OUTPUT_FORMAT_JSON = 'json'; |
|
40 | - public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty'; |
|
41 | - |
|
42 | - protected string $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN; |
|
43 | - private bool $php_pcntl_signal = false; |
|
44 | - private bool $interrupted = false; |
|
45 | - |
|
46 | - protected function configure() { |
|
47 | - $this |
|
48 | - ->addOption( |
|
49 | - 'output', |
|
50 | - null, |
|
51 | - InputOption::VALUE_OPTIONAL, |
|
52 | - 'Output format (plain, json or json_pretty, default is plain)', |
|
53 | - $this->defaultOutputFormat |
|
54 | - ) |
|
55 | - ; |
|
56 | - } |
|
57 | - |
|
58 | - protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, array $items, string $prefix = ' - '): void { |
|
59 | - switch ($input->getOption('output')) { |
|
60 | - case self::OUTPUT_FORMAT_JSON: |
|
61 | - $output->writeln(json_encode($items)); |
|
62 | - break; |
|
63 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
64 | - $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
65 | - break; |
|
66 | - default: |
|
67 | - foreach ($items as $key => $item) { |
|
68 | - if (is_array($item)) { |
|
69 | - $output->writeln($prefix . $key . ':'); |
|
70 | - $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix); |
|
71 | - continue; |
|
72 | - } |
|
73 | - if (!is_int($key) || ListCommand::class === get_class($this)) { |
|
74 | - $value = $this->valueToString($item); |
|
75 | - if (!is_null($value)) { |
|
76 | - $output->writeln($prefix . $key . ': ' . $value); |
|
77 | - } else { |
|
78 | - $output->writeln($prefix . $key); |
|
79 | - } |
|
80 | - } else { |
|
81 | - $output->writeln($prefix . $this->valueToString($item)); |
|
82 | - } |
|
83 | - } |
|
84 | - break; |
|
85 | - } |
|
86 | - } |
|
87 | - |
|
88 | - protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void { |
|
89 | - switch ($input->getOption('output')) { |
|
90 | - case self::OUTPUT_FORMAT_JSON: |
|
91 | - $output->writeln(json_encode($items)); |
|
92 | - break; |
|
93 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
94 | - $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
95 | - break; |
|
96 | - default: |
|
97 | - $table = new Table($output); |
|
98 | - $table->setRows($items); |
|
99 | - if (!empty($items) && is_string(array_key_first(reset($items)))) { |
|
100 | - $table->setHeaders(array_keys(reset($items))); |
|
101 | - } |
|
102 | - $table->render(); |
|
103 | - break; |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - |
|
108 | - /** |
|
109 | - * @param mixed $item |
|
110 | - */ |
|
111 | - protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) { |
|
112 | - if (is_array($item)) { |
|
113 | - $this->writeArrayInOutputFormat($input, $output, $item, ''); |
|
114 | - return; |
|
115 | - } |
|
116 | - |
|
117 | - switch ($input->getOption('output')) { |
|
118 | - case self::OUTPUT_FORMAT_JSON: |
|
119 | - $output->writeln(json_encode($item)); |
|
120 | - break; |
|
121 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
122 | - $output->writeln(json_encode($item, JSON_PRETTY_PRINT)); |
|
123 | - break; |
|
124 | - default: |
|
125 | - $output->writeln($this->valueToString($item, false)); |
|
126 | - break; |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - protected function valueToString($value, bool $returnNull = true): ?string { |
|
131 | - if ($value === false) { |
|
132 | - return 'false'; |
|
133 | - } elseif ($value === true) { |
|
134 | - return 'true'; |
|
135 | - } elseif ($value === null) { |
|
136 | - return $returnNull ? null : 'null'; |
|
137 | - } else { |
|
138 | - return $value; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Throw InterruptedException when interrupted by user |
|
144 | - * |
|
145 | - * @throws InterruptedException |
|
146 | - */ |
|
147 | - protected function abortIfInterrupted() { |
|
148 | - if ($this->php_pcntl_signal === false) { |
|
149 | - return; |
|
150 | - } |
|
151 | - |
|
152 | - pcntl_signal_dispatch(); |
|
153 | - |
|
154 | - if ($this->interrupted === true) { |
|
155 | - throw new InterruptedException('Command interrupted by user'); |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Changes the status of the command to "interrupted" if ctrl-c has been pressed |
|
161 | - * |
|
162 | - * Gives a chance to the command to properly terminate what it's doing |
|
163 | - */ |
|
164 | - protected function cancelOperation() { |
|
165 | - $this->interrupted = true; |
|
166 | - } |
|
167 | - |
|
168 | - public function run(InputInterface $input, OutputInterface $output) { |
|
169 | - // check if the php pcntl_signal functions are accessible |
|
170 | - $this->php_pcntl_signal = function_exists('pcntl_signal'); |
|
171 | - if ($this->php_pcntl_signal) { |
|
172 | - // Collect interrupts and notify the running command |
|
173 | - pcntl_signal(SIGTERM, [$this, 'cancelOperation']); |
|
174 | - pcntl_signal(SIGINT, [$this, 'cancelOperation']); |
|
175 | - } |
|
176 | - |
|
177 | - return parent::run($input, $output); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @param string $optionName |
|
182 | - * @param CompletionContext $context |
|
183 | - * @return string[] |
|
184 | - */ |
|
185 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
186 | - if ($optionName === 'output') { |
|
187 | - return ['plain', 'json', 'json_pretty']; |
|
188 | - } |
|
189 | - return []; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @param string $argumentName |
|
194 | - * @param CompletionContext $context |
|
195 | - * @return string[] |
|
196 | - */ |
|
197 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
198 | - return []; |
|
199 | - } |
|
38 | + public const OUTPUT_FORMAT_PLAIN = 'plain'; |
|
39 | + public const OUTPUT_FORMAT_JSON = 'json'; |
|
40 | + public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty'; |
|
41 | + |
|
42 | + protected string $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN; |
|
43 | + private bool $php_pcntl_signal = false; |
|
44 | + private bool $interrupted = false; |
|
45 | + |
|
46 | + protected function configure() { |
|
47 | + $this |
|
48 | + ->addOption( |
|
49 | + 'output', |
|
50 | + null, |
|
51 | + InputOption::VALUE_OPTIONAL, |
|
52 | + 'Output format (plain, json or json_pretty, default is plain)', |
|
53 | + $this->defaultOutputFormat |
|
54 | + ) |
|
55 | + ; |
|
56 | + } |
|
57 | + |
|
58 | + protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, array $items, string $prefix = ' - '): void { |
|
59 | + switch ($input->getOption('output')) { |
|
60 | + case self::OUTPUT_FORMAT_JSON: |
|
61 | + $output->writeln(json_encode($items)); |
|
62 | + break; |
|
63 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
64 | + $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
65 | + break; |
|
66 | + default: |
|
67 | + foreach ($items as $key => $item) { |
|
68 | + if (is_array($item)) { |
|
69 | + $output->writeln($prefix . $key . ':'); |
|
70 | + $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix); |
|
71 | + continue; |
|
72 | + } |
|
73 | + if (!is_int($key) || ListCommand::class === get_class($this)) { |
|
74 | + $value = $this->valueToString($item); |
|
75 | + if (!is_null($value)) { |
|
76 | + $output->writeln($prefix . $key . ': ' . $value); |
|
77 | + } else { |
|
78 | + $output->writeln($prefix . $key); |
|
79 | + } |
|
80 | + } else { |
|
81 | + $output->writeln($prefix . $this->valueToString($item)); |
|
82 | + } |
|
83 | + } |
|
84 | + break; |
|
85 | + } |
|
86 | + } |
|
87 | + |
|
88 | + protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void { |
|
89 | + switch ($input->getOption('output')) { |
|
90 | + case self::OUTPUT_FORMAT_JSON: |
|
91 | + $output->writeln(json_encode($items)); |
|
92 | + break; |
|
93 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
94 | + $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
95 | + break; |
|
96 | + default: |
|
97 | + $table = new Table($output); |
|
98 | + $table->setRows($items); |
|
99 | + if (!empty($items) && is_string(array_key_first(reset($items)))) { |
|
100 | + $table->setHeaders(array_keys(reset($items))); |
|
101 | + } |
|
102 | + $table->render(); |
|
103 | + break; |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + |
|
108 | + /** |
|
109 | + * @param mixed $item |
|
110 | + */ |
|
111 | + protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) { |
|
112 | + if (is_array($item)) { |
|
113 | + $this->writeArrayInOutputFormat($input, $output, $item, ''); |
|
114 | + return; |
|
115 | + } |
|
116 | + |
|
117 | + switch ($input->getOption('output')) { |
|
118 | + case self::OUTPUT_FORMAT_JSON: |
|
119 | + $output->writeln(json_encode($item)); |
|
120 | + break; |
|
121 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
122 | + $output->writeln(json_encode($item, JSON_PRETTY_PRINT)); |
|
123 | + break; |
|
124 | + default: |
|
125 | + $output->writeln($this->valueToString($item, false)); |
|
126 | + break; |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + protected function valueToString($value, bool $returnNull = true): ?string { |
|
131 | + if ($value === false) { |
|
132 | + return 'false'; |
|
133 | + } elseif ($value === true) { |
|
134 | + return 'true'; |
|
135 | + } elseif ($value === null) { |
|
136 | + return $returnNull ? null : 'null'; |
|
137 | + } else { |
|
138 | + return $value; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Throw InterruptedException when interrupted by user |
|
144 | + * |
|
145 | + * @throws InterruptedException |
|
146 | + */ |
|
147 | + protected function abortIfInterrupted() { |
|
148 | + if ($this->php_pcntl_signal === false) { |
|
149 | + return; |
|
150 | + } |
|
151 | + |
|
152 | + pcntl_signal_dispatch(); |
|
153 | + |
|
154 | + if ($this->interrupted === true) { |
|
155 | + throw new InterruptedException('Command interrupted by user'); |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Changes the status of the command to "interrupted" if ctrl-c has been pressed |
|
161 | + * |
|
162 | + * Gives a chance to the command to properly terminate what it's doing |
|
163 | + */ |
|
164 | + protected function cancelOperation() { |
|
165 | + $this->interrupted = true; |
|
166 | + } |
|
167 | + |
|
168 | + public function run(InputInterface $input, OutputInterface $output) { |
|
169 | + // check if the php pcntl_signal functions are accessible |
|
170 | + $this->php_pcntl_signal = function_exists('pcntl_signal'); |
|
171 | + if ($this->php_pcntl_signal) { |
|
172 | + // Collect interrupts and notify the running command |
|
173 | + pcntl_signal(SIGTERM, [$this, 'cancelOperation']); |
|
174 | + pcntl_signal(SIGINT, [$this, 'cancelOperation']); |
|
175 | + } |
|
176 | + |
|
177 | + return parent::run($input, $output); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @param string $optionName |
|
182 | + * @param CompletionContext $context |
|
183 | + * @return string[] |
|
184 | + */ |
|
185 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
186 | + if ($optionName === 'output') { |
|
187 | + return ['plain', 'json', 'json_pretty']; |
|
188 | + } |
|
189 | + return []; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @param string $argumentName |
|
194 | + * @param CompletionContext $context |
|
195 | + * @return string[] |
|
196 | + */ |
|
197 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
198 | + return []; |
|
199 | + } |
|
200 | 200 | } |
@@ -30,72 +30,72 @@ |
||
30 | 30 | |
31 | 31 | class FileChanges implements IFilter { |
32 | 32 | |
33 | - /** @var IL10N */ |
|
34 | - protected $l; |
|
33 | + /** @var IL10N */ |
|
34 | + protected $l; |
|
35 | 35 | |
36 | - /** @var IURLGenerator */ |
|
37 | - protected $url; |
|
36 | + /** @var IURLGenerator */ |
|
37 | + protected $url; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param IL10N $l |
|
41 | - * @param IURLGenerator $url |
|
42 | - */ |
|
43 | - public function __construct(IL10N $l, IURLGenerator $url) { |
|
44 | - $this->l = $l; |
|
45 | - $this->url = $url; |
|
46 | - } |
|
39 | + /** |
|
40 | + * @param IL10N $l |
|
41 | + * @param IURLGenerator $url |
|
42 | + */ |
|
43 | + public function __construct(IL10N $l, IURLGenerator $url) { |
|
44 | + $this->l = $l; |
|
45 | + $this->url = $url; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return string Lowercase a-z only identifier |
|
50 | - * @since 11.0.0 |
|
51 | - */ |
|
52 | - public function getIdentifier() { |
|
53 | - return 'files'; |
|
54 | - } |
|
48 | + /** |
|
49 | + * @return string Lowercase a-z only identifier |
|
50 | + * @since 11.0.0 |
|
51 | + */ |
|
52 | + public function getIdentifier() { |
|
53 | + return 'files'; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return string A translated string |
|
58 | - * @since 11.0.0 |
|
59 | - */ |
|
60 | - public function getName() { |
|
61 | - return $this->l->t('File changes'); |
|
62 | - } |
|
56 | + /** |
|
57 | + * @return string A translated string |
|
58 | + * @since 11.0.0 |
|
59 | + */ |
|
60 | + public function getName() { |
|
61 | + return $this->l->t('File changes'); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return int |
|
66 | - * @since 11.0.0 |
|
67 | - */ |
|
68 | - public function getPriority() { |
|
69 | - return 30; |
|
70 | - } |
|
64 | + /** |
|
65 | + * @return int |
|
66 | + * @since 11.0.0 |
|
67 | + */ |
|
68 | + public function getPriority() { |
|
69 | + return 30; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * @return string Full URL to an icon, empty string when none is given |
|
74 | - * @since 11.0.0 |
|
75 | - */ |
|
76 | - public function getIcon() { |
|
77 | - return $this->url->getAbsoluteURL($this->url->imagePath('core', 'places/files.svg')); |
|
78 | - } |
|
72 | + /** |
|
73 | + * @return string Full URL to an icon, empty string when none is given |
|
74 | + * @since 11.0.0 |
|
75 | + */ |
|
76 | + public function getIcon() { |
|
77 | + return $this->url->getAbsoluteURL($this->url->imagePath('core', 'places/files.svg')); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * @param string[] $types |
|
82 | - * @return string[] An array of allowed apps from which activities should be displayed |
|
83 | - * @since 11.0.0 |
|
84 | - */ |
|
85 | - public function filterTypes(array $types) { |
|
86 | - return array_intersect([ |
|
87 | - 'file_created', |
|
88 | - 'file_changed', |
|
89 | - 'file_deleted', |
|
90 | - 'file_restored', |
|
91 | - ], $types); |
|
92 | - } |
|
80 | + /** |
|
81 | + * @param string[] $types |
|
82 | + * @return string[] An array of allowed apps from which activities should be displayed |
|
83 | + * @since 11.0.0 |
|
84 | + */ |
|
85 | + public function filterTypes(array $types) { |
|
86 | + return array_intersect([ |
|
87 | + 'file_created', |
|
88 | + 'file_changed', |
|
89 | + 'file_deleted', |
|
90 | + 'file_restored', |
|
91 | + ], $types); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @return string[] An array of allowed apps from which activities should be displayed |
|
96 | - * @since 11.0.0 |
|
97 | - */ |
|
98 | - public function allowedApps() { |
|
99 | - return ['files']; |
|
100 | - } |
|
94 | + /** |
|
95 | + * @return string[] An array of allowed apps from which activities should be displayed |
|
96 | + * @since 11.0.0 |
|
97 | + */ |
|
98 | + public function allowedApps() { |
|
99 | + return ['files']; |
|
100 | + } |
|
101 | 101 | } |