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

132 lines
3.8 KiB
PHP

<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Authentication\UserGroup;
use Icinga\Data\Filter\Filter;
use Icinga\Repository\DbRepository;
use Icinga\User;
class DbUserGroupBackend extends DbRepository implements UserGroupBackendInterface
{
/**
* The query columns being provided
*
* @var array
*/
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)'
),
'group_membership' => array(
'group' => 'group_name COLLATE utf8_general_ci',
'group_name',
'user' => 'username COLLATE utf8_general_ci',
'user_name' => 'username',
'created_at' => 'UNIX_TIMESTAMP(ctime)',
'last_modified' => 'UNIX_TIMESTAMP(mtime)'
)
);
/**
* The statement columns being provided
*
* @var array
*/
protected $statementColumns = array(
'group' => array(
'created_at' => 'ctime',
'last_modified' => 'mtime'
),
'group_membership' => array(
'created_at' => 'ctime',
'last_modified' => 'mtime'
)
);
/**
* The columns which are not permitted to be queried
*
* @var array
*/
protected $filterColumns = array('group', 'parent', 'user');
/**
* Initialize this database user group backend
*/
protected function init()
{
if (! $this->ds->getTablePrefix()) {
$this->ds->setTablePrefix('icingaweb_');
}
}
/**
* Insert a table row with the given data
*
* @param string $table
* @param array $bind
*/
public function insert($table, array $bind)
{
$bind['created_at'] = date('Y-m-d H:i:s');
parent::insert($table, $bind);
}
/**
* Update table rows with the given data, optionally limited by using a filter
*
* @param string $table
* @param array $bind
* @param Filter $filter
*/
public function update($table, array $bind, Filter $filter = null)
{
$bind['last_modified'] = date('Y-m-d H:i:s');
parent::update($table, $bind, $filter);
}
/**
* Return the groups the given user is a member of
*
* @param User $user
*
* @return array
*/
public function getMemberships(User $user)
{
$groupStmt = $this->ds->select()
->from($this->prependTablePrefix('group'), array('name', 'parent'))
->getSelectQuery()
->query();
$groups = array();
foreach ($groupStmt as $group) {
$groups[$group->name] = $group->parent;
}
$membershipStmt = $this->ds->select() // TODO: Join this table
->from($this->prependTablePrefix('group_membership'), array('group_name'))
->where('username', $user->getUsername())
->getSelectQuery()
->query();
$memberships = array();
foreach ($membershipStmt as $membership) {
$memberships[] = $membership->group_name;
$parent = $groups[$membership->group_name];
while ($parent !== null) {
$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;
}
}
return $memberships;
}
}