Software Development Engineer

Blog PostsResume

ownCloud 10.0 Feature: Allow using email address for password recovery

ownCloud is a free and open source suite of client-server software for creating file hosting services and using them. ownCloud, A personal cloud which runs on your server, has its core backend implemented in PHP and uses HTML, CSS, and jQuery on its front end.

In ownCloud, it is optional to have an email address for a user. One caveat here is that the user cannot request a password reset mail whenever he forgets his password if he hadn't registered an email address. Whenever a user fails a login attempt, an option is enabled which lets the user reset his password. On clicking that link, an email is sent to the registered email address of the user whose username is inputted in the name field. Since ownCloud allowed its users to login via the registered email address, whenever a user fails an attempt while logging in and then requests for the password recovery mail, ownCloud fails to act upon that request. Having the functionality of letting a user use an email address for password recovery is beneficial as it makes the user experience more friendly. A requirement for such an enhancement was pointed out in an issue raised: #27111.

Reading the issue and thinking that I will be able to resolve it, I started working on a solution. Giving my first pull request, it was pointed out to me that I had introduced a fair bit of redundancy. Removing the redundancy, I didn't realize that I'll have to make updates to the unit tests. Never have I ever worked on Unit Tests before and hence it required me to learn PHPUnit. After pushing the tests my solution got merged to the master and the feature got mentioned in ownCloud 10.0 features.

Implementation

When a password reset was requested, the input field was checked only for the existence of a user with the given input as username, whereas the login feature considered the input as either username or email address. So a condition to check the given input as email address was added. Since ownCloud allows many users to have the same email address, a check was also made for the count of users found. An email was sent only when the email address was unique to a single user.

$users = $this->userManager->getByEmail($user);
switch (count($users)) {
  case 0:
    $this->logger->error('Could not send reset email because User does not exist. User: {user}', ['app' => 'core', 'user' => $user]);
    return false;
 case 1:
    $this->logger->info('User with input as email address found. User: {user}', ['app' => 'core', 'user' => $user]);
    $email = $users[0]->getEMailAddress();
    $user = $users[0]->getUID();
    break;
 default:
    $this->logger->error('Could not send reset email because the email id is not unique. User: {user}', ['app' => 'core', 'user' => $user]);
    return false;
}

© 2024 Ujjwal Bhardwaj. All Rights Reserved.