icingaweb2/library/Icinga/Authentication/UserGroup/DbUserGroupBackend.php

92 lines
2.6 KiB
PHP
Raw Normal View History

2014-10-20 13:42:33 +02:00
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
2014-10-20 13:42:33 +02:00
namespace Icinga\Authentication\UserGroup;
2014-10-20 13:42:33 +02:00
use Icinga\Repository\DbRepository;
2014-10-20 13:42:33 +02:00
use Icinga\User;
class DbUserGroupBackend extends DbRepository implements UserGroupBackendInterface
2014-10-20 13:42:33 +02:00
{
/**
* The query columns being provided
2014-10-20 13:42:33 +02:00
*
* @var array
2014-10-20 13:42:33 +02:00
*/
protected $queryColumns = array(
'group' => array(
'group' => 'name COLLATE utf8_general_ci',
'group_name' => 'name',
'parent' => 'parent COLLATE utf8_general_ci',
'parent_name' => 'parent',
'created_at' => 'UNIX_TIMESTAMP(ctime)',
'last_modified' => 'UNIX_TIMESTAMP(mtime)'
)
);
2014-10-20 13:42:33 +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
/**
* The default sort rules to be applied on a query
2014-10-20 13:42:33 +02:00
*
* @var array
2014-10-20 13:42:33 +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
{
if (! $this->ds->getTablePrefix()) {
$this->ds->setTablePrefix('icingaweb_');
}
2014-10-20 13:42:33 +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();
$groupsStmt = $this->select(array('group_name', 'parent_name'))->getQuery()->getSelectQuery()->query();
2014-10-20 13:42:33 +02:00
foreach ($groupsStmt as $group) {
$groups[$group->group_name] = $group->parent_name;
2014-10-20 13:42:33 +02:00
}
2014-10-20 13:42:33 +02:00
$memberships = array();
$membershipsStmt = $this->ds->getDbAdapter() // TODO: Use the join feature, once available
2014-10-20 13:42:33 +02:00
->select()
->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];
while ($parent !== null) {
2014-10-20 13:42:33 +02:00
$memberships[] = $parent;
// 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
}
}
2014-10-20 13:42:33 +02:00
return $memberships;
}
}