Implement mapping of user's home directory

Signed-off-by: Daniel Klaffenbach <daniel.klaffenbach@hrz.tu-chemnitz.de>
This commit is contained in:
Daniel Klaffenbach 2018-04-19 14:12:41 +02:00
parent 24883d3c66
commit 624d1a23b9
8 changed files with 68 additions and 6 deletions

View file

@ -1,6 +1,14 @@
# Changelog
All notable changes to this project will be documented in this file.
## 2.0.1
### Changed
- add attribute mapping for the users home directory when creating a new user
## 2.0.0
### Changed

View file

@ -25,6 +25,15 @@
<notnull>true</notnull>
<length>255</length>
</field>
<field>
<name>home</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>255</length>
</field>
</declaration>
</table>

View file

@ -16,7 +16,7 @@ The following providers are supported and tested at the moment:
* Any other provider that authenticates using the environment variable
While theoretically any other authentication provider implementing either one of those standards is compatible, we like to note that they are not part of any internal test matrix.]]></description>
<version>2.0.0</version>
<version>2.0.1</version>
<licence>agpl</licence>
<author>Lukas Reschke</author>
<namespace>User_SAML</namespace>

View file

@ -148,7 +148,7 @@ class SAMLController extends Controller {
}
throw new NoUserFoundException('Auto provisioning not allowed and user ' . $uid . ' does not exist');
} elseif(!$userExists && $autoProvisioningAllowed) {
$this->userBackend->createUserIfNotExists($uid);
$this->userBackend->createUserIfNotExists($uid, $auth);
$this->userBackend->updateAttributes($uid, $auth);
return;
}

View file

@ -120,6 +120,12 @@ class Admin implements ISettings {
'type' => 'line',
'required' => true,
],
'home_mapping' => [
'text' => $this->l10n->t('Attribute to map the users home to.'),
'type' => 'line',
'required' => true,
],
];
$type = $this->config->getAppValue('user_saml', 'type');

View file

@ -105,16 +105,29 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
}
/**
* Creates an user if it does not exists
* Creates a user if it does not exist. In case home directory mapping
* is enabled we also set up the user's home from $attributes.
*
* @param string $uid
* @param array $attributes
*/
public function createUserIfNotExists($uid) {
public function createUserIfNotExists($uid, array $attributes = array()) {
if(!$this->userExistsInDatabase($uid)) {
$values = [
'uid' => $uid,
];
// Try to get the mapped home directory of the user
try {
$home = $this->getAttributeValue('saml-attribute-mapping-home_mapping', $attributes);
} catch (\InvalidArgumentException $e) {
$home = null;
}
if ($home !== null) {
$values['home'] = $home;
}
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->insert('user_saml_users');
@ -150,6 +163,7 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
public function implementsActions($actions) {
$availableActions = \OC\User\Backend::CHECK_PASSWORD;
$availableActions |= \OC\User\Backend::GET_DISPLAYNAME;
$availableActions |= \OC\User\Backend::GET_HOME;
return (bool)($availableActions & $actions);
}
@ -200,6 +214,27 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
return false;
}
/**
* Returns the user's home directory, if home directory mapping is set up.
*
* @param string $uid the username
* @return string
*/
public function getHome($uid) {
if($this->userExistsInDatabase($uid)) {
$qb = $this->db->getQueryBuilder();
$qb->select('home')
->from('user_saml_users')
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
->setMaxResults(1);
$result = $qb->execute();
$users = $result->fetchAll();
if (isset($users[0]['home'])) {
return $users[0]['home'];
}
}
}
/**
* Get a list of all users
*
@ -596,7 +631,6 @@ class UserBackend implements IApacheBackend, UserInterface, IUserBackend {
$newGroups = null;
}
if ($user !== null) {
$currentEmail = (string)$user->getEMailAddress();
if ($newEmail !== null

View file

@ -340,7 +340,7 @@ class SAMLControllerTest extends TestCase {
->method('createUserIfNotExists')
->with('MyUid');
$this->userBackend
->expects($this->once())
->expects($this->exactly(2))
->method('getCurrentUserId')
->willReturn('MyUid');
$this->userManager

View file

@ -129,6 +129,11 @@ class AdminTest extends \Test\TestCase {
'type' => 'line',
'required' => true,
],
'home_mapping' => [
'text' => $this->l10n->t('Attribute to map the users home to.'),
'type' => 'line',
'required' => true,
],
];
$params = [