Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically set user email and display name #12

Open
ThisIsQasim opened this issue Jul 26, 2018 · 11 comments · May be fixed by #263 or #265
Open

Automatically set user email and display name #12

ThisIsQasim opened this issue Jul 26, 2018 · 11 comments · May be fixed by #263 or #265
Labels
enhancement New feature or request

Comments

@ThisIsQasim
Copy link

When using External User Backend with IMAP the display name is set as the email address and the user's email is not set. Can we add the ability to set the display name as the name before the @ in the email address and the user's email address automatically setup?

@MariusBluem MariusBluem transferred this issue from nextcloud/apps Dec 20, 2018
@violoncelloCH violoncelloCH added the enhancement New feature or request label Dec 21, 2018
@violoncelloCH
Copy link
Member

Yes, this would be possible!
The email can be set automatically if the IMAP backend is used.
For the display name I'm not that sure. This could be confusing if there are users on different domains
with the same "first part of the mail". On the other hand if manually set, users can have the same display name as well.
PR is welcome!

@violoncelloCH
Copy link
Member

related to #10

@ghost
Copy link

ghost commented Jan 17, 2020

@violoncelloCH Can you point out where to start? Which file to edit.

@violoncelloCH
Copy link
Member

violoncelloCH commented Feb 3, 2020

hi @benkees
sorry for the late answer (have been busy myself)...
the relevant files in user_external to edit would be the /lib/base.php (specifically those two functions:

user_external/lib/base.php

Lines 146 to 194 in f5629eb

/**
* Change the display name of a user
*
* @param string $uid The username
* @param string $displayName The new display name
*
* @return true/false
*/
public function setDisplayName($uid, $displayName) {
if (!$this->userExists($uid)) {
return false;
}
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->update('users_external')
->set('displayname', $query->createNamedParameter($displayName))
->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
->andWhere($query->expr()->eq('backend', $query->createNamedParameter($this->backend)));
$query->execute();
return true;
}
/**
* Create user record in database
*
* @param string $uid The username
* @param array $groups Groups to add the user to on creation
*
* @return void
*/
protected function storeUser($uid, $groups = []) {
if (!$this->userExists($uid)) {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->insert('users_external')
->values([
'uid' => $query->createNamedParameter($uid),
'backend' => $query->createNamedParameter($this->backend),
]);
$query->execute();
if ($groups) {
$createduser = \OC::$server->getUserManager()->get($uid);
foreach ($groups as $group) {
\OC::$server->getGroupManager()->createGroup($group)->addUser($createduser);
}
}
}
}
) and the /lib/imap.php (check out this call specifically where the user is created:
$this->storeUser($uid, $groups);
)

for reference in nextcloud/server especially this function in the IUser interface seems to be relevant: https://github.com/nextcloud/server/blob/906348ca145058c68f66237de487436575570a44/lib/public/IUser.php#L188-L195
as well as this interface here: https://github.com/nextcloud/server/blob/master/lib/public/User/Backend/ISetDisplayNameBackend.php

don't hesitate to ask further questions :) (I hope I'll manage to answer them faster now as I've a little more spare time the coming weeks)

@violoncelloCH
Copy link
Member

sorry, didn't mean to close it 🙃

@mmccarn
Copy link

mmccarn commented Nov 8, 2020

If 'stripeDomain' is FALSE, we can set the email in lib/imap.php pretty simply:

--- user_external/lib/imap.php	2020-11-08 14:19:02.524859391 -0500
+++ ./imap.php	2020-11-08 14:16:48.550661342 -0500
@@ -103,6 +103,13 @@
 			curl_close($ch);
 			$uid = mb_strtolower($uid);
 			$this->storeUser($uid, $groups);
+                        if ( ! $this->stripeDomain) {
+                          /**
+                          * Set the IMAP user's email to their login
+                          */
+                           $config = \OC::$server->getConfig();
+                           $config->setUserValue( $uid, 'settings', 'email', $uid);
+                        }
 			return $uid;
 		} else {
 			OC::$server->getLogger()->error(

However, a "full" implementation would support the situation where 'stripeDomain' is TRUE, or the situation where the email address is different than the authenticating domain - which I see either requiring new configuration options or making assumptions that might be wrong for some users.

        private $mailbox;
        private $port;
        private $sslmode;
        private $domain;
        private $stripeDomain;
        private $groupDomain;
        private $storeEmail;
        private $emailDomain;

With apologies for the pseudo-code -

  • (do we need to do this?) If we want an admin to be able to specify an email domain that is different from the authentication domain -

    • set $myemail to $pieces[0] . '@' . $emailDomain (if $emailDomain is not empty)
    • set $myemail to $pieces[0] . '@' . $domain (if $domain is not empty)
    • otherwise, set $myemail to '$uid' if $uid contains an '@' sign
  • If we're willing to cause problems for people who want the email domain to be different than the authentication domain:

    • if $uid as entered contains an '@', save it to '$myemail' before splitting $uid into pieces
    • if $uid as entered does not contain '@', but '$domain' is set, set $myemail to $uid . '@' . $domain
  • Finally, add these lines after 'storeUser' to save the email address

    • We could decide that $storeEmail is TRUE unless the admin specifically adds it as FALSE
if ( $storeEmail) {
$config = \OC::$server->getConfig();
$config->setUserValue( $uid, 'settings', 'email', $myemail);
}

@almereyda
Copy link

I'm happy to test out solutions to this issue, as it fits a local use case.

Can we produce a PR from the snippets given here, and iterate from there?

@violoncelloCH
Copy link
Member

fine with me, @almereyda

@Mannshoch
Copy link

Would that feature work together with the Nextcloud Mail app so that new user e.g. automatically get access to their E-Mails?

@motherboard1999
Copy link

@almereyda any updates?

@almereyda
Copy link

As far as I can see there is currently no PR associated to this issue.

@Mannshoch Mannshoch mentioned this issue May 11, 2022
@brknkfr brknkfr linked a pull request Feb 1, 2025 that will close this issue
@K-os K-os linked a pull request Feb 11, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
6 participants