Skip to content

Commit df2eb96

Browse files
committed
Merge pull request #24389 from owncloud/login-by-email
Allow login by email address
2 parents 0629378 + 7aca13f commit df2eb96

File tree

7 files changed

+60
-5
lines changed

7 files changed

+60
-5
lines changed

apps/dav/lib/connector/sabre/principal.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof')
196196
* @return string
197197
*/
198198
function findByUri($uri, $principalPrefix) {
199+
if (substr($uri, 0, 7) === 'mailto:') {
200+
$email = substr($uri, 7);
201+
$users = $this->userManager->getByEmail($email);
202+
if (count($users) === 1) {
203+
return $this->principalPrefix . '/' . $users[0]->getUID();
204+
}
205+
}
206+
199207
return '';
200208
}
201209

apps/dav/lib/server.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public function __construct(IRequest $request, $baseUri) {
8484

8585
// acl
8686
$acl = new DavAclPlugin();
87+
$acl->principalCollectionSet = [
88+
'principals/users', 'principals/groups'
89+
];
8790
$acl->defaultUsernamePath = 'principals/users';
8891
$this->server->addPlugin($acl);
8992

apps/dav/tests/unit/connector/sabre/principal.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,19 @@ public function testUpdatePrincipal() {
255255
public function testSearchPrincipals() {
256256
$this->assertSame([], $this->connector->searchPrincipals('principals/users', []));
257257
}
258+
259+
public function testFindByUri() {
260+
$fooUser = $this->getMockBuilder('\OC\User\User')
261+
->disableOriginalConstructor()->getMock();
262+
$fooUser
263+
->expects($this->exactly(1))
264+
->method('getUID')
265+
->will($this->returnValue('foo'));
266+
267+
$this->userManager->expects($this->once())->method('getByEmail')->willReturn([
268+
$fooUser
269+
]);
270+
$ret = $this->connector->findByUri('mailto:foo@bar.net', 'principals/users');
271+
$this->assertSame('principals/users/foo', $ret);
272+
}
258273
}

core/templates/login.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
</div>
4141
<p class="grouptop">
4242
<input type="text" name="user" id="user"
43-
placeholder="<?php p($l->t('Username')); ?>"
43+
placeholder="<?php p($l->t('Username or email')); ?>"
4444
value="<?php p($_['loginName']); ?>"
4545
<?php p($_['user_autofocus'] ? 'autofocus' : ''); ?>
4646
autocomplete="on" autocapitalize="off" autocorrect="off" required>
47-
<label for="user" class="infield"><?php p($l->t('Username')); ?></label>
47+
<label for="user" class="infield"><?php p($l->t('Username or email')); ?></label>
4848
</p>
4949

5050
<p class="groupbottom">

lib/private/legacy/user.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,22 @@ public static function setupBackends() {
152152
/**
153153
* Try to login a user
154154
*
155-
* @param string $loginname The login name of the user to log in
155+
* @param string $loginName The login name of the user to log in
156156
* @param string $password The password of the user
157157
* @return boolean|null
158158
*
159159
* Log in a user and regenerate a new session - if the password is ok
160160
*/
161-
public static function login($loginname, $password) {
162-
$result = self::getUserSession()->login($loginname, $password);
161+
public static function login($loginName, $password) {
162+
163+
$result = self::getUserSession()->login($loginName, $password);
164+
if (!$result) {
165+
$users = \OC::$server->getUserManager()->getByEmail($loginName);
166+
// we only allow login by email if unique
167+
if (count($users) === 1) {
168+
$result = self::getUserSession()->login($users[0]->getUID(), $password);
169+
}
170+
}
163171
if ($result) {
164172
// Refresh the token
165173
\OC::$server->getCsrfTokenManager()->refreshToken();

lib/private/user/manager.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
namespace OC\User;
3434

3535
use OC\Hooks\PublicEmitter;
36+
use OCP\IUser;
3637
use OCP\IUserBackend;
3738
use OCP\IUserManager;
3839
use OCP\IConfig;
@@ -354,4 +355,17 @@ public function callForAllUsers(\Closure $callback, $search = '') {
354355
} while (count($users) >= $limit);
355356
}
356357
}
358+
359+
/**
360+
* @param string $email
361+
* @return IUser[]
362+
* @since 9.1.0
363+
*/
364+
public function getByEmail($email) {
365+
$userIds = $this->config->getUsersForUserValue('settings', 'email', $email);
366+
367+
return array_map(function($uid) {
368+
return $this->get($uid);
369+
}, $userIds);
370+
}
357371
}

lib/public/iusermanager.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,11 @@ public function countUsers();
142142
* @since 9.0.0
143143
*/
144144
public function callForAllUsers (\Closure $callback, $search = '');
145+
146+
/**
147+
* @param string $email
148+
* @return IUser[]
149+
* @since 9.1.0
150+
*/
151+
public function getByEmail($email);
145152
}

0 commit comments

Comments
 (0)