Skip to content

Commit

Permalink
add email query support (closes PanCakeConnaisseur#20)
Browse files Browse the repository at this point in the history
add group support (closes PanCakeConnaisseur#7)
  • Loading branch information
kosli committed Apr 8, 2020
1 parent 1d697ee commit c4a66e4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Config {
const CONFIG_KEY_COUNT_USERS = 'count_users';
const CONFIG_KEY_GET_HOME = 'get_home';
const CONFIG_KEY_CREATE_USER = 'create_user';
const CONFIG_KEY_GET_EMAIL_ADDRESS = 'get_email_address';
const CONFIG_KEY_GET_GROUPS = 'get_groups';

private $logger;
private $appConfiguration;
Expand Down Expand Up @@ -218,6 +220,14 @@ public function getQueryCreateUser() {
return $this->getQueryStringOrFalse(self::CONFIG_KEY_CREATE_USER);
}

public function getQueryGetEmailAddress() {
return $this->getQueryStringOrFalse(self::CONFIG_KEY_GET_EMAIL_ADDRESS);
}

public function getQueryGetGroups() {
return $this->getQueryStringOrFalse(self::CONFIG_KEY_GET_GROUPS);
}

/**
* Tries to read a config value and throws an exception if it is not set.
* This is used for config keys that are mandatory.
Expand Down
60 changes: 59 additions & 1 deletion lib/UserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@

use OCP\ILogger;
use OC\User\Backend;
use OCP\IUserManager;
use OCP\IGroupManager;

class UserBackend implements \OCP\IUserBackend, \OCP\UserInterface {

private $logger;
private $logContext = ['app' => 'user_backend_sql_raw'];
private $config;
private $db;
private $userManager;
private $groupManager;

public function __construct(ILogger $logger, Config $config, Db $db) {
public function __construct(ILogger $logger, Config $config, Db $db, IUserManager $userManager, IGroupManager $groupManager) {
$this->logger = $logger;
$this->config = $config;
// Don't get db handle (dbo object) here yet, so that it is only created
// when db queries are actually run.
$this->db = $db;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
}

public function getBackendName() {
Expand Down Expand Up @@ -135,6 +141,8 @@ public function getDisplayName($providedUsername) {
$statement = $this->db->getDbHandle()->prepare($this->config->getQueryGetDisplayName());
$statement->execute(['username' => $providedUsername]);
$retrievedDisplayName = $statement->fetchColumn();

$this->updateAttributes($providedUsername);
return $retrievedDisplayName;
}

Expand Down Expand Up @@ -259,6 +267,56 @@ public function createUser($providedUsername, $providedPassword) {
}
}

public function updateAttributes($providedUsername) {
$retrievedEmailAddress = null;
if(!empty($this->config->getQueryGetEmailAddress())) {
$statement = $this->db->getDbHandle()->prepare($this->config->getQueryGetEmailAddress());
$statement->execute(['username' => $providedUsername]);
$newEmailAddress = $statement->fetchColumn();
}
$user = $this->userManager->get($providedUsername);

$newGroups = null;
if(!empty($this->config->getQueryGetGroups())) {
$statement = $this->db->getDbHandle()->prepare($this->config->getQueryGetGroups());
$statement->execute(['username' => $providedUsername]);
$newGroups = $statement->fetchAll(\PDO::FETCH_COLUMN, 0);

// Make sure that the user is always in the "everyone" group
if(!in_array('everyone', $newGroups)) {
$newGroups[] = 'everyone';
}
}

if ($user !== null) {
$currentEmailAddress = (string)$user->getEMailAddress();
if ($newEmailAddress !== null
&& $currentEmailAddress !== $newEmailAddress) {
$user->setEMailAddress($newEmailAddress);
}

if ($newGroups !== null) {
$groupManager = $this->groupManager;
$oldGroups = $groupManager->getUserGroupIds($user);

$groupsToAdd = array_unique(array_diff($newGroups, $oldGroups));
$groupsToRemove = array_diff($oldGroups, $newGroups);

foreach ($groupsToAdd as $group) {
if (!($groupManager->groupExists($group))) {
$groupManager->createGroup($group);
}
$groupManager->get($group)->addUser($user);
}

foreach ($groupsToRemove as $group) {
$groupManager->get($group)->removeUser($user);
}
}
}
}


/**
* Escape % and _ with \.
*
Expand Down

0 comments on commit c4a66e4

Please sign in to comment.