2014-10-20 13:42:15 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-10-20 13:42:15 +02:00
|
|
|
|
2015-04-21 12:38:57 +02:00
|
|
|
namespace Icinga\Authentication\UserGroup;
|
2014-10-20 13:42:15 +02:00
|
|
|
|
2015-05-05 15:24:18 +02:00
|
|
|
use Icinga\Repository\Repository;
|
2014-10-20 13:42:15 +02:00
|
|
|
use Icinga\User;
|
|
|
|
use Icinga\Util\String;
|
|
|
|
|
2015-05-05 15:24:18 +02:00
|
|
|
class IniUserGroupBackend extends Repository implements UserGroupBackendInterface
|
2014-10-20 13:42:15 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-05-05 15:24:18 +02:00
|
|
|
* The query columns being provided
|
2014-10-20 13:42:15 +02:00
|
|
|
*
|
2015-05-05 15:24:18 +02:00
|
|
|
* @var array
|
2014-10-20 13:42:15 +02:00
|
|
|
*/
|
2015-05-05 15:24:18 +02:00
|
|
|
protected $queryColumns = array(
|
|
|
|
'groups' => array(
|
|
|
|
'group' => 'name',
|
|
|
|
'group_name' => 'name',
|
|
|
|
'parent' => 'parent',
|
|
|
|
'parent_name' => 'parent',
|
|
|
|
'created_at' => 'ctime',
|
|
|
|
'last_modified' => 'mtime',
|
|
|
|
'users'
|
|
|
|
)
|
|
|
|
);
|
2014-10-20 13:42:15 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-05 15:24:18 +02:00
|
|
|
* The columns which are not permitted to be queried
|
2014-10-20 13:42:15 +02:00
|
|
|
*
|
2015-05-05 15:24:18 +02:00
|
|
|
* @var array
|
2014-10-20 13:42:15 +02:00
|
|
|
*/
|
2015-05-05 15:24:18 +02:00
|
|
|
protected $filterColumns = array('group', 'parent');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default sort rules to be applied on a query
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $sortRules = array(
|
|
|
|
'group_name' => array(
|
|
|
|
'columns' => array(
|
|
|
|
'group_name',
|
|
|
|
'parent_name'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-10-20 13:42:15 +02:00
|
|
|
|
2015-05-06 08:41:54 +02:00
|
|
|
/**
|
|
|
|
* Initialize this ini user group backend
|
|
|
|
*/
|
|
|
|
protected function init()
|
|
|
|
{
|
|
|
|
$this->ds->getConfigObject()->setKeyColumn('name');
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:42:15 +02:00
|
|
|
/**
|
2015-05-05 15:24:18 +02:00
|
|
|
* Return the groups the given user is a member of
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*
|
|
|
|
* @return array
|
2014-10-20 13:42:15 +02:00
|
|
|
*/
|
|
|
|
public function getMemberships(User $user)
|
|
|
|
{
|
2015-05-05 15:24:18 +02:00
|
|
|
$result = $this->select()->fetchAll();
|
|
|
|
|
2014-10-20 13:42:15 +02:00
|
|
|
$groups = array();
|
2015-05-05 15:24:18 +02:00
|
|
|
foreach ($result as $group) {
|
2015-05-06 08:41:54 +02:00
|
|
|
$groups[$group->group_name] = $group->parent_name;
|
2015-05-05 15:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$username = strtolower($user->getUsername());
|
|
|
|
$memberships = array();
|
|
|
|
foreach ($result as $group) {
|
2015-05-06 08:41:54 +02:00
|
|
|
if ($group->users && !in_array($group->group_name, $memberships)) {
|
2015-05-05 15:24:18 +02:00
|
|
|
$users = array_map('strtolower', String::trimSplit($group->users));
|
2015-05-06 08:41:54 +02:00
|
|
|
if (in_array($username, $users)) {
|
2015-05-05 15:24:18 +02:00
|
|
|
$memberships[] = $group->group_name;
|
|
|
|
$parent = $groups[$group->group_name];
|
|
|
|
while ($parent !== null) {
|
|
|
|
$memberships[] = $parent;
|
|
|
|
$parent = isset($groups[$parent]) ? $groups[$parent] : null;
|
|
|
|
}
|
|
|
|
}
|
2014-10-20 13:42:15 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-05 15:24:18 +02:00
|
|
|
|
|
|
|
return $memberships;
|
2014-10-20 13:42:15 +02:00
|
|
|
}
|
|
|
|
}
|