2014-10-20 13:42:33 +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:33 +02:00
|
|
|
|
2015-04-21 12:38:57 +02:00
|
|
|
namespace Icinga\Authentication\UserGroup;
|
2014-10-20 13:42:33 +02:00
|
|
|
|
2015-05-05 09:23:29 +02:00
|
|
|
use Icinga\Repository\DbRepository;
|
2014-10-20 13:42:33 +02:00
|
|
|
use Icinga\User;
|
|
|
|
|
2015-05-05 09:23:29 +02:00
|
|
|
class DbUserGroupBackend extends DbRepository implements UserGroupBackendInterface
|
2014-10-20 13:42:33 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-05-05 09:23:29 +02:00
|
|
|
* The query columns being provided
|
2014-10-20 13:42:33 +02:00
|
|
|
*
|
2015-05-05 09:23:29 +02:00
|
|
|
* @var array
|
2014-10-20 13:42:33 +02:00
|
|
|
*/
|
2015-05-05 09:23:29 +02:00
|
|
|
protected $queryColumns = array(
|
|
|
|
'group' => array(
|
2015-05-05 09:34:49 +02:00
|
|
|
'group' => 'name COLLATE utf8_general_ci',
|
2015-05-05 09:23:29 +02:00
|
|
|
'group_name' => 'name',
|
2015-05-05 09:34:49 +02:00
|
|
|
'parent' => 'parent COLLATE utf8_general_ci',
|
2015-05-05 09:23:29 +02:00
|
|
|
'parent_name' => 'parent',
|
|
|
|
'created_at' => 'UNIX_TIMESTAMP(ctime)',
|
|
|
|
'last_modified' => 'UNIX_TIMESTAMP(mtime)'
|
|
|
|
)
|
|
|
|
);
|
2014-10-20 13:42:33 +02:00
|
|
|
|
2015-05-05 09:34:49 +02:00
|
|
|
/**
|
|
|
|
* The columns which are not permitted to be queried
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $filterColumns = array('group', 'parent');
|
|
|
|
|
2014-10-20 13:42:33 +02:00
|
|
|
/**
|
2015-05-05 09:23:29 +02:00
|
|
|
* The default sort rules to be applied on a query
|
2014-10-20 13:42:33 +02:00
|
|
|
*
|
2015-05-05 09:23:29 +02:00
|
|
|
* @var array
|
2014-10-20 13:42:33 +02:00
|
|
|
*/
|
2015-05-05 09:23:29 +02:00
|
|
|
protected $sortRules = array(
|
|
|
|
'group_name' => array(
|
|
|
|
'columns' => array(
|
|
|
|
'group_name',
|
|
|
|
'parent_name'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize this database user group backend
|
|
|
|
*/
|
|
|
|
protected function init()
|
2014-10-20 13:42:33 +02:00
|
|
|
{
|
2015-05-05 09:23:29 +02:00
|
|
|
if (! $this->ds->getTablePrefix()) {
|
|
|
|
$this->ds->setTablePrefix('icingaweb_');
|
|
|
|
}
|
2014-10-20 13:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-05 09:23:29 +02:00
|
|
|
* Return the groups the given user is a member of
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*
|
|
|
|
* @return array
|
2014-10-20 13:42:33 +02:00
|
|
|
*/
|
|
|
|
public function getMemberships(User $user)
|
|
|
|
{
|
|
|
|
$groups = array();
|
2015-05-05 09:23:29 +02:00
|
|
|
$groupsStmt = $this->select(array('group_name', 'parent_name'))->getQuery()->getSelectQuery()->query();
|
2014-10-20 13:42:33 +02:00
|
|
|
foreach ($groupsStmt as $group) {
|
2015-05-05 09:23:29 +02:00
|
|
|
$groups[$group->group_name] = $group->parent_name;
|
2014-10-20 13:42:33 +02:00
|
|
|
}
|
2015-05-05 09:23:29 +02:00
|
|
|
|
2014-10-20 13:42:33 +02:00
|
|
|
$memberships = array();
|
2015-05-05 09:23:29 +02:00
|
|
|
$membershipsStmt = $this->ds->getDbAdapter() // TODO: Use the join feature, once available
|
2014-10-20 13:42:33 +02:00
|
|
|
->select()
|
2015-05-05 09:23:29 +02:00
|
|
|
->from($this->ds->getTablePrefix() . 'group_membership', array('group_name'))
|
2014-10-20 13:42:33 +02:00
|
|
|
->where('username = ?', $user->getUsername())
|
|
|
|
->query();
|
|
|
|
foreach ($membershipsStmt as $membership) {
|
|
|
|
$memberships[] = $membership->group_name;
|
|
|
|
$parent = $groups[$membership->group_name];
|
2015-05-05 09:23:29 +02:00
|
|
|
while ($parent !== null) {
|
2014-10-20 13:42:33 +02:00
|
|
|
$memberships[] = $parent;
|
2015-05-05 09:23:29 +02:00
|
|
|
// Usually a parent is an existing group, but since we do not have a constraint on our table..
|
|
|
|
$parent = isset($groups[$parent]) ? $groups[$parent] : null;
|
2014-10-20 13:42:33 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-05 09:23:29 +02:00
|
|
|
|
2014-10-20 13:42:33 +02:00
|
|
|
return $memberships;
|
|
|
|
}
|
|
|
|
}
|