Skip to content

Commit

Permalink
Merge pull request #24389 from owncloud/login-by-email
Browse files Browse the repository at this point in the history
Allow login by email address
  • Loading branch information
LukasReschke committed May 3, 2016
2 parents 0629378 + 7aca13f commit df2eb96
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
8 changes: 8 additions & 0 deletions apps/dav/lib/connector/sabre/principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof')
* @return string
*/
function findByUri($uri, $principalPrefix) {
if (substr($uri, 0, 7) === 'mailto:') {
$email = substr($uri, 7);
$users = $this->userManager->getByEmail($email);
if (count($users) === 1) {
return $this->principalPrefix . '/' . $users[0]->getUID();
}
}

return '';
}

Expand Down
3 changes: 3 additions & 0 deletions apps/dav/lib/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public function __construct(IRequest $request, $baseUri) {

// acl
$acl = new DavAclPlugin();
$acl->principalCollectionSet = [
'principals/users', 'principals/groups'
];
$acl->defaultUsernamePath = 'principals/users';
$this->server->addPlugin($acl);

Expand Down
15 changes: 15 additions & 0 deletions apps/dav/tests/unit/connector/sabre/principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,19 @@ public function testUpdatePrincipal() {
public function testSearchPrincipals() {
$this->assertSame([], $this->connector->searchPrincipals('principals/users', []));
}

public function testFindByUri() {
$fooUser = $this->getMockBuilder('\OC\User\User')
->disableOriginalConstructor()->getMock();
$fooUser
->expects($this->exactly(1))
->method('getUID')
->will($this->returnValue('foo'));

$this->userManager->expects($this->once())->method('getByEmail')->willReturn([
$fooUser
]);
$ret = $this->connector->findByUri('mailto:foo@bar.net', 'principals/users');
$this->assertSame('principals/users/foo', $ret);
}
}
4 changes: 2 additions & 2 deletions core/templates/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
</div>
<p class="grouptop">
<input type="text" name="user" id="user"
placeholder="<?php p($l->t('Username')); ?>"
placeholder="<?php p($l->t('Username or email')); ?>"
value="<?php p($_['loginName']); ?>"
<?php p($_['user_autofocus'] ? 'autofocus' : ''); ?>
autocomplete="on" autocapitalize="off" autocorrect="off" required>
<label for="user" class="infield"><?php p($l->t('Username')); ?></label>
<label for="user" class="infield"><?php p($l->t('Username or email')); ?></label>
</p>

<p class="groupbottom">
Expand Down
14 changes: 11 additions & 3 deletions lib/private/legacy/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,22 @@ public static function setupBackends() {
/**
* Try to login a user
*
* @param string $loginname The login name of the user to log in
* @param string $loginName The login name of the user to log in
* @param string $password The password of the user
* @return boolean|null
*
* Log in a user and regenerate a new session - if the password is ok
*/
public static function login($loginname, $password) {
$result = self::getUserSession()->login($loginname, $password);
public static function login($loginName, $password) {

$result = self::getUserSession()->login($loginName, $password);
if (!$result) {
$users = \OC::$server->getUserManager()->getByEmail($loginName);
// we only allow login by email if unique
if (count($users) === 1) {
$result = self::getUserSession()->login($users[0]->getUID(), $password);
}
}
if ($result) {
// Refresh the token
\OC::$server->getCsrfTokenManager()->refreshToken();
Expand Down
14 changes: 14 additions & 0 deletions lib/private/user/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
namespace OC\User;

use OC\Hooks\PublicEmitter;
use OCP\IUser;
use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\IConfig;
Expand Down Expand Up @@ -354,4 +355,17 @@ public function callForAllUsers(\Closure $callback, $search = '') {
} while (count($users) >= $limit);
}
}

/**
* @param string $email
* @return IUser[]
* @since 9.1.0
*/
public function getByEmail($email) {
$userIds = $this->config->getUsersForUserValue('settings', 'email', $email);

return array_map(function($uid) {
return $this->get($uid);
}, $userIds);
}
}
7 changes: 7 additions & 0 deletions lib/public/iusermanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,11 @@ public function countUsers();
* @since 9.0.0
*/
public function callForAllUsers (\Closure $callback, $search = '');

/**
* @param string $email
* @return IUser[]
* @since 9.1.0
*/
public function getByEmail($email);
}

0 comments on commit df2eb96

Please sign in to comment.