Skip to content
Snippets Groups Projects
Commit b70d08e8 authored by Tim Steiner's avatar Tim Steiner
Browse files

User admin has been re-implemented at /auth/user-admin

parent 922911be
Branches
Tags
No related merge requests found
<?php
class Auth_UserAdminController extends App_Controller_Action
{
public function indexAction()
{
$users = Auth_UserModel::findAll();
$groups = Auth_GroupModel::findAll(false);
$groupChildGroups = Auth_GroupModel::findByParentGroup($groups, false);
$groupParentGroups = Auth_GroupModel::findByChildGroup($groups, false);
$userGroups = Auth_GroupModel::findByUser($users, false);
$groupUsers = Auth_UserModel::findByParentGroup($groups, false);
$this->view->users = $users;
$this->view->groups = $groups;
$this->view->userGroups = $userGroups;
$this->view->groupChildGroups = $groupChildGroups;
$this->view->groupParentGroups = $groupParentGroups;
$this->view->groupUsers = $groupUsers;
}
public function editUserAction()
{
$user = Auth_UserModel::find($this->_getParam('id'));
$groups = Auth_GroupModel::findAll(false);
$userGroups = Auth_GroupModel::findByUser($user, false);
$groups->orderBy('getName');
$groupsArray = array();
foreach ($groups as $group) {
$groupsArray[$group->getId()] = $group->getName();
}
$userGroupsArray = array();
foreach ($userGroups as $group) {
$userGroupsArray[] = $group->getId();
}
$this->view->user = $user;
$this->view->groups = $groupsArray;
$this->view->userGroups = $userGroupsArray;
}
public function editUserPostAction()
{
$in = $this->_getAllParams();
print_r($in);
$user = Auth_UserModel::find($in['id']);
if (!$user) {
$user = Auth_UserModel::fetchNew();
}
$user->setFirstName($in['firstName']);
$user->setMiddleName($in['middleName']);
$user->setLastName($in['lastName']);
$user->setEmail($in['email']);
$user->setPhone($in['phone']);
Auth_UserModel::save($user);
$parentGroups = Auth_GroupModel::find($in['groups']);
$userGroupMap = array($user->getId() => $parentGroups);
Auth_GroupModel::setUserParentGroups($userGroupMap);
$this->_disableLayoutAndView();
$this->_redirect('/auth/user-admin/edit-user/id/' . $user->getId());
}
public function editGroupAction()
{
$group = Auth_GroupModel::find($this->_getParam('id'));
$this->view->group = $group;
$groups = Auth_GroupModel::findAll(false);
$parentGroups = Auth_GroupModel::findByChildGroup($group, false);
$childGroups = Auth_GroupModel::findByParentGroup($group, false);
$userMembers = Auth_UserModel::findByParentGroup($group, false);
$users = Auth_UserModel::findAll();
$groups->orderBy('getName');
$users->orderBy('getUsername');
$groupsArray = array();
foreach ($groups as $group) {
$groupsArray[$group->getId()] = $group->getName();
}
$usersArray = array();
foreach ($users as $user) {
$usersArray[$user->getId()] = $user->getUsername();
}
$parentGroupsArray = array();
foreach ($parentGroups as $group) {
$parentGroupsArray[] = $group->getId();
}
$childGroupsArray = array();
foreach ($childGroups as $group) {
$childGroupsArray[] = $group->getId();
}
$userMembersArray = array();
foreach ($userMembers as $user) {
$userMembersArray[] = $user->getId();
}
$this->view->users = $usersArray;
$this->view->groups = $groupsArray;
$this->view->parentGroups = $parentGroupsArray;
$this->view->childGroups = $childGroupsArray;
$this->view->userMembers = $userMembersArray;
}
public function editGroupPostAction()
{
$in = $this->_getAllParams();
$group = Auth_GroupModel::find($in['id']);
if (!$group) {
$group = Auth_GroupModel::fetchNew();
}
$group->setName($in['name']);
$group->setDescription($in['description']);
Auth_GroupModel::save($group);
$parentGroups = Auth_GroupModel::find((array) $in['parentGroups']);
$childGroups = Auth_GroupModel::find((array) $in['childGroups']);
$users = Auth_UserModel::find((array) $in['users']);
$parentGroupMap = array();
$parentGroupMap[$group->getId()] = $parentGroups;
Auth_GroupModel::setGroupParentGroups($parentGroupMap);
$childGroupMap = array();
$childGroupMap[$group->getId()] = $childGroups;
Auth_GroupModel::setGroupChildGroups($childGroupMap);
$userMap = array();
$userMap[$group->getId()] = $users;
Auth_GroupModel::setGroupUserMembers($userMap);
$this->_redirect('/auth/user-admin/edit-group/id/' . $group->getId());
}
}
\ No newline at end of file
...@@ -2,7 +2,65 @@ ...@@ -2,7 +2,65 @@
class Auth_GroupModel extends Unl_Model class Auth_GroupModel extends Unl_Model
{ {
public static function findByUser($user) public static function find($id)
{
if (Unl_Util::isArray($id) && count($id) == 0) {
return new Unl_Model_Collection(__CLASS__);
}
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
$select->from(array('g' => 'creqGroups'));
if (Unl_Util::isArray($id)) {
$select->where('g.groupId IN(?)', $id);
} else {
$select->where('g.groupId = ?', $id);
}
$records = $db->query($select)->fetchAll();
$objects = new Unl_Model_Collection(__CLASS__);
foreach ($records as $record) {
$object = Unl_Model_Registry::getInstance()->getOrAdd(new self($record));
$objectId = $object->getId();
$objects[$objectId] = $object;
}
if (Unl_Util::isArray($id)) {
return $objects;
} else {
return $objects->pop();
}
}
static public function findAll($includePrimary = true)
{
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
$select->from(array('g' => 'creqGroups'));
$select->where('type = 1');
if (!$includePrimary) {
$select->joinLeft(array('u' => 'creqUsers'), 'u.primaryGroup = g.groupId', array('userId'));
$select->where('ISNULL(u.userId)');
}
$records = $select->query()->fetchAll();
$groupIds = array();
foreach ($records as $record) {
$groupIds[] = $record['groupId'];
}
return self::find($groupIds);
}
/**
* Returns a list of groups that the user(s) is/are a member of
*
* @param Auth_UserModel|Unl_Model_Collection $user
* @param bool[optional] $implicitMemberships
* @return Unl_Model_Collection
*/
static public function findByUser($user, $implicitMemberships = true)
{ {
$db = Zend_Registry::get('db'); $db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db); $select = new Zend_Db_Select($db);
...@@ -47,6 +105,9 @@ class Auth_GroupModel extends Unl_Model ...@@ -47,6 +105,9 @@ class Auth_GroupModel extends Unl_Model
$select->from(array('u' => 'creqUsers'), array('userId')); $select->from(array('u' => 'creqUsers'), array('userId'));
$select->join(array('m' => 'creqGroupImpliedMemberships'), 'u.primaryGroup = m.childGroup', array()); $select->join(array('m' => 'creqGroupImpliedMemberships'), 'u.primaryGroup = m.childGroup', array());
$select->join(array('g' => 'creqGroups'), 'm.parentGroup = g.groupId'); $select->join(array('g' => 'creqGroups'), 'm.parentGroup = g.groupId');
if (!$implicitMemberships) {
$select->where("m.explicit = 'yes'");
}
if (Unl_Util::isArray($userId)) { if (Unl_Util::isArray($userId)) {
$select->where('u.userId IN (?)', $userId); $select->where('u.userId IN (?)', $userId);
} else { } else {
...@@ -69,8 +130,553 @@ class Auth_GroupModel extends Unl_Model ...@@ -69,8 +130,553 @@ class Auth_GroupModel extends Unl_Model
} }
} }
/**
* Returns a collection of the groups that the provided group(s) is/are a member of.
*
* @param Auth_GroupModel|Unl_Model_Collection $group
* @param bool[optional] $impliedMemberships
* @return Unl_Model_Collection
*/
static public function findByChildGroup($group, $impliedMemberships = true)
{
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
$select->from(array('m' => 'creqGroupImpliedMemberships'), array('childGroup', 'parentGroup'));
$select->where('m.type = 1');
if (!$impliedMemberships) {
$select->where("m.explicit = 'yes'");
}
if (Unl_Util::isArray($group)) {
if (count($group) == 0) {
return new Unl_Model_Collection(__CLASS__);
}
$select->where('childGroup IN(?)', $group->getId());
} else {
$select->where('childGroup = ?', $group->getId());
}
$records = $select->query()->fetchAll();
$parentGroupIds = array();
foreach ($records as $record) {
$parentGroupIds[] = $record['parentGroup'];
}
self::find($parentGroupIds);
$parentGroups = array();
if (Unl_Util::isArray($group)) {
foreach ($group->getId() as $groupId) {
$parentGroups[$groupId] = new Unl_Model_Collection(__CLASS__);
}
} else {
$parentGroups[$group->getId()] = new Unl_Model_Collection(__CLASS__);
}
foreach ($records as $record) {
$parentGroups[$record['childGroup']][$record['parentGroup']] = Unl_Model_Registry::getInstance()->get(__CLASS__, $record['parentGroup']);
}
if (Unl_Util::isArray($group)) {
return $parentGroups;
} else {
return $parentGroups[$group->getId()];
}
}
/**
* Returns a collection of the groups that are members of the given group(s).
*
* @param Auth_GroupModel|Unl_Model_Collection $group
* @param bool[optional] $impliedMemberships
* @return Unl_Model_Collection
*/
static public function findByParentGroup($group, $impliedMemberships = true, $primaryGroups = false)
{
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
$select->from(array('m' => 'creqGroupImpliedMemberships'), array('childGroup', 'parentGroup'));
$select->joinLeft(array('u' => 'creqUsers'), 'm.childGroup = u.primaryGroup', array());
$select->where('m.type = 1');
if (!$primaryGroups) {
$select->where('u.userId IS NULL');
}
if (!$impliedMemberships) {
$select->where("m.explicit = 'yes'");
}
if (Unl_Util::isArray($group)) {
if (count($group) == 0) {
return new Unl_Model_Collection(__CLASS__);
}
$select->where('parentGroup IN(?)', $group->getId());
} else {
$select->where('parentGroup = ?', $group->getId());
}
$records = $select->query()->fetchAll();
$childGroupIds = array();
foreach ($records as $record) {
$childGroupIds[] = $record['childGroup'];
}
self::find($childGroupIds);
$childGroups = array();
if (Unl_Util::isArray($group)) {
foreach ($group->getId() as $groupId) {
$childGroups[$groupId] = new Unl_Model_Collection(__CLASS__);
}
} else {
$childGroups[$group->getId()] = new Unl_Model_Collection(__CLASS__);
}
foreach ($records as $record) {
$childGroups[$record['parentGroup']][$record['childGroup']] = Unl_Model_Registry::getInstance()->get(__CLASS__, $record['childGroup']);
}
if (Unl_Util::isArray($group)) {
return $childGroups;
} else {
return $childGroups[$group->getId()];
}
}
static public function fetchNew()
{
$data = array(
'groupId' => null,
'type' => 1,
'name' => null,
'description' => null
);
$new = new self($data);
return $new;
}
static public function save($models)
{
if (!Unl_Util::isArray($models)) {
$collection = new Unl_Model_Collection(__CLASS__);
$collection[] = $models;
$models = $collection;
}
$db = Zend_Registry::get('db');
$db->beginTransaction();
$unsavedData = array();
foreach ($models as $id => $model) {
$unsavedData[$id] = $model->_data;
}
try {
$modelsToInsert = new Unl_Model_Collection(__CLASS__);
$modelsToUpdate = new Unl_Model_Collection(__CLASS__);
foreach ($models as $model) {
if ($model->getId()) {
$modelsToUpdate[] = $model;
} else {
$modelsToInsert[] = $model;
}
}
self::_insert($modelsToInsert);
self::_update($modelsToUpdate);
$db->commit();
} catch (Exception $e) {
$db->rollback();
foreach ($models as $id => $model) {
$model->_data = $unsavedData[$id];
}
throw $e;
}
}
static protected function _update(Unl_Model_Collection $models)
{
if (count($models) == 0) {
return;
}
$db = Zend_Registry::get('db');
$sql = 'CREATE TEMPORARY TABLE creqGroupsUpdate '
. 'SELECT * FROM creqGroups LIMIT 0';
$db->query($sql);
$sql = 'INSERT INTO creqGroupsUpdate VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['groupId'])
. $db->quoteInto('?, ' , $model->_data['type'])
. $db->quoteInto('?, ' , $model->_data['name'])
. $db->quoteInto('?)' , $model->_data['description']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
$sql = 'UPDATE creqGroups AS a, '
. ' creqGroupsUpdate AS b '
. 'SET a.name = b.name, '
. ' a.type = b.type, '
. ' a.description = b.description '
. 'WHERE a.groupId = b.groupId ';
$db->query($sql);
$db->query('DROP TABLE creqGroupsUpdate');
}
static protected function _insert(Unl_Model_Collection $models)
{
if (count($models) == 0) {
return;
}
$db = Zend_Registry::get('db');
$sql = 'INSERT INTO creqGroups (type, name, description) VALUES ';
$sqlParts = array();
foreach ($models as $model) {
$sqlParts[] = $db->quoteInto('(?, ', $model->_data['type'])
. $db->quoteInto('?, ' , $model->_data['name'])
. $db->quoteInto('?)' , $model->_data['description']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
$lastId = $db->lastInsertId();
foreach ($models as $model) {
$model->_data['groupId'] = $lastId;
$lastId++;
}
}
/**
* Sets the groups that the supplied user is a member of.
* The supplied array should have one key for each user (the primary key)
* and a collection of parentGroups for each value
*
* @param array $groupMap
*/
static public function setUserParentGroups($userGroupMap)
{
$userIds = array();
foreach ($userGroupMap as $userId => $targetGroups) {
$userIds[] = $userId;
}
$users = Auth_UserModel::find($userIds);
$groupMap = array();
foreach ($userGroupMap as $userId => $targetGroups) {
$user = $users[$userId];
$primaryGroup = $user->getPrimaryGroup();
$groupMap[$primaryGroup] = $targetGroups;
}
self::setGroupParentGroups($groupMap);
}
/**
* Sets the groups that the supplied group is a member of.
* The supplied array should have one key for each childGroup (the primary key)
* and a collection of parentGroups for each value
*
* @param array $groupMap
*/
static public function setGroupParentGroups($groupMap)
{
$childGroupIds = array();
foreach ($groupMap as $childGroupId => $targetParentGroups) {
$childGroupIds[] = $childGroupId;
}
$childGroups = self::find($childGroupIds);
$childGroupParentGroups = self::findByChildGroup($childGroups, false);
$membershipsToAdd = array();
$membershipsToRemove = array();
foreach ($childGroups as $childGroup) {
$membershipsToAdd[$childGroup->getId()] = new Unl_Model_Collection(__CLASS__);
$membershipsToRemove[$childGroup->getId()] = new Unl_Model_Collection(__CLASS__);
}
foreach ($groupMap as $childGroupId => $targetParentGroups) {
$currentParentGroups = $childGroupParentGroups[$childGroupId];
foreach ($currentParentGroups as $currentParentGroup) {
if (!in_array($currentParentGroup->getId(), $targetParentGroups->getId())) {
$membershipsToRemove[$childGroupId][] = $currentParentGroup;
}
}
foreach ($targetParentGroups as $targetParentGroup) {
if (!in_array($targetParentGroup->getId(), $currentParentGroups->getId())) {
$membershipsToAdd[$childGroupId][] = $targetParentGroup;
}
}
}
self::addGroupParentGroups($membershipsToAdd);
self::removeGroupParentGroups($membershipsToRemove);
}
/**
* Adds the supplied child group to additional parent groups.
* The supplied array should have one key for each childGroup (the primary key)
* and a collection of parentGroups for each value
*
* @param array $groupMap
*/
static public function addGroupParentGroups($groupMap)
{
$membershipMap = array();
foreach ($groupMap as $childGroupId => $newParentGroups) {
foreach ($newParentGroups as $newParentGroup) {
$membershipMap[] = array('childGroupId' => $childGroupId, 'parentGroupId' => $newParentGroup->getId());
}
}
if (count($membershipMap) == 0) {
return;
}
$db = Zend_Registry::get('db');
$sql = 'INSERT INTO creqGroupMemberships (type, parentGroup, childGroup) VALUES ';
$sqlParts = array();
foreach ($membershipMap as $membership) {
$sqlParts[] = $db->quoteInto('(?, ', 1)
. $db->quoteInto('?, ' , $membership['parentGroupId'])
. $db->quoteInto('?)' , $membership['childGroupId']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
}
/**
* Removes the supplied child group from the respective parent groups.
* The supplied array should have one key for each childGroup (the primary key)
* and a collection of parentGroups for each value
*
* @param array $groupMap
*/
static public function removeGroupParentGroups($groupMap)
{
$membershipMap = array();
foreach ($groupMap as $childGroupId => $oldParentGroups) {
foreach ($oldParentGroups as $oldParentGroup) {
$membershipMap[] = array('childGroupId' => $childGroupId, 'parentGroupId' => $oldParentGroup->getId());
}
}
if (count($membershipMap) == 0) {
return;
}
$db = Zend_Registry::get('db');
$sql = 'DELETE FROM creqGroupMemberships WHERE ';
$sqlParts = array();
foreach ($membershipMap as $membership) {
$sqlParts[] = $db->quoteInto('(type = 1 AND ')
. $db->quoteInto('parentGroup = ? AND ' , $membership['parentGroupId'])
. $db->quoteInto('childGroup = ?)' , $membership['childGroupId']);
}
$sql .= implode(' OR ', $sqlParts);
$db->query($sql);
}
/**
* Sets the users that are members of the supplied group.
* The supplied array should have one key for each parentGroup (the primary key)
* and a collection of users for each value
*
* @param array $groupMap
*/
static public function setGroupUserMembers($groupUserMap)
{
$parentGroupIds = array();
$primaryGroupIds = array();
foreach ($groupUserMap as $parentGroupId => $targetUsers) {
$parentGroupIds[] = $parentGroupId;
foreach ($targetUsers as $targetUser) {
$primaryGroupIds[] = $targetUser->getPrimaryGroup();
}
}
$parentGroups = self::find($parentGroupIds);
$parentGroupUsers = Auth_UserModel::findByParentGroup($parentGroups, false);
foreach ($parentGroupUsers as $aParentGroupUsers) {
foreach ($aParentGroupUsers as $parentGroupUser) {
$primaryGroupIds[] = $parentGroupUser->getPrimaryGroup();
}
}
$primaryGroups = self::find($primaryGroupIds);
$membershipsToAdd = array();
$membershipsToRemove = array();
foreach ($parentGroups as $parentGroup) {
$membershipsToAdd[$parentGroup->getId()] = new Unl_Model_Collection(__CLASS__);
$membershipsToRemove[$parentGroup->getId()] = new Unl_Model_Collection(__CLASS__);
}
foreach ($groupUserMap as $parentGroupId => $targetUsers) {
$currentUsers = $parentGroupUsers[$parentGroupId];
foreach ($currentUsers as $currentUser) {
if (!in_array($currentUser->getPrimaryGroup(), $targetUsers->arrayFromMethod('getPrimaryGroup'))) {
$membershipsToRemove[$parentGroupId][] = $primaryGroups[$currentUser->getPrimaryGroup()];
}
}
foreach ($targetUsers as $targetUser) {
if (!in_array($targetUser->getPrimaryGroup(), $currentUsers->arrayFromMethod('getPrimaryGroup'))) {
$membershipsToAdd[$parentGroupId][] = $primaryGroups[$targetUser->getPrimaryGroup()];
}
}
}
self::addGroupChildGroups($membershipsToAdd);
self::removeGroupChildGroups($membershipsToRemove);
}
/**
* Sets the groups that are members of the given parentGroup.
* The supplied array should have one key for each parentGroup (the primary key)
* and a collection of childGroups for each value
*
* @param array $groupMap
*/
static public function setGroupChildGroups($groupMap)
{
$parentGroupIds = array();
foreach ($groupMap as $parentGroupId => $targetChildGroups) {
$parentGroupIds[] = $parentGroupId;
}
$parentGroups = self::find($parentGroupIds);
$parentGroupChildGroups = self::findByParentGroup($parentGroups, false);
$membershipsToAdd = array();
$membershipsToRemove = array();
foreach ($parentGroups as $parentGroup) {
$membershipsToAdd[$parentGroup->getId()] = new Unl_Model_Collection(__CLASS__);
$membershipsToRemove[$parentGroup->getId()] = new Unl_Model_Collection(__CLASS__);
}
foreach ($groupMap as $parentGroupId => $targetChildGroups) {
$currentChildGroups = $parentGroupChildGroups[$parentGroupId];
foreach ($currentChildGroups as $currentChildGroup) {
if (!in_array($currentChildGroup->getId(), $targetChildGroups->getId())) {
$membershipsToRemove[$parentGroupId][] = $currentChildGroup;
}
}
foreach ($targetChildGroups as $targetChildGroup) {
if (!in_array($targetChildGroup->getId(), $currentChildGroups->getId())) {
$membershipsToAdd[$parentGroupId][] = $targetChildGroup;
}
}
}
self::addGroupChildGroups($membershipsToAdd);
self::removeGroupChildGroups($membershipsToRemove);
}
/**
* Adds additional child groups to the supplied parentGroup.
* The supplied array should have one key for each parentGroup (the primary key)
* and a collection of childGroups for each value
*
* @param array $groupMap
*/
static public function addGroupChildGroups($groupMap)
{
$membershipMap = array();
foreach ($groupMap as $parentGroupId => $newChildGroups) {
foreach ($newChildGroups as $newChildGroup) {
$membershipMap[] = array('parentGroupId' => $parentGroupId, 'childGroupId' => $newChildGroup->getId());
}
}
if (count($membershipMap) == 0) {
return;
}
$db = Zend_Registry::get('db');
$sql = 'INSERT INTO creqGroupMemberships (type, parentGroup, childGroup) VALUES ';
$sqlParts = array();
foreach ($membershipMap as $membership) {
$sqlParts[] = $db->quoteInto('(?, ', 1)
. $db->quoteInto('?, ' , $membership['parentGroupId'])
. $db->quoteInto('?)' , $membership['childGroupId']);
}
$sql .= implode(', ', $sqlParts);
$db->query($sql);
}
/**
* Removes the respective child groups from the supplied parent group.
* The supplied array should have one key for each parentGroup (the primary key)
* and a collection of childGroups for each value
*
* @param array $groupMap
*/
static public function removeGroupChildGroups($groupMap)
{
$membershipMap = array();
foreach ($groupMap as $parentGroupId => $oldChildGroups) {
foreach ($oldChildGroups as $oldChildGroup) {
$membershipMap[] = array('parentGroupId' => $parentGroupId, 'childGroupId' => $oldChildGroup->getId());
}
}
if (count($membershipMap) == 0) {
return;
}
$db = Zend_Registry::get('db');
$sql = 'DELETE FROM creqGroupMemberships WHERE ';
$sqlParts = array();
foreach ($membershipMap as $membership) {
$sqlParts[] = $db->quoteInto('(type = 1 AND ')
. $db->quoteInto('parentGroup = ? AND ' , $membership['parentGroupId'])
. $db->quoteInto('childGroup = ?)' , $membership['childGroupId']);
}
$sql .= implode(' OR ', $sqlParts);
$db->query($sql);
}
public function getId() public function getId()
{ {
return $this->_data['groupId']; return $this->_data['groupId'];
} }
public function getName()
{
return $this->_data['name'];
}
public function setName($name)
{
$this->_data['name'] = $name;
}
public function getDescription()
{
return $this->_data['description'];
}
public function setDescription($description)
{
$this->_data['description'] = $description;
}
} }
...@@ -9,8 +9,12 @@ class Auth_UserModel extends Unl_Model { ...@@ -9,8 +9,12 @@ class Auth_UserModel extends Unl_Model {
*/ */
static protected $_ldap; static protected $_ldap;
public static function find($id) static public function find($id)
{ {
if (count($id) == 0) {
return new Unl_Model_Collection(__CLASS__);
}
$db = Zend_Registry::get('db'); $db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db); $select = new Zend_Db_Select($db);
...@@ -82,7 +86,7 @@ class Auth_UserModel extends Unl_Model { ...@@ -82,7 +86,7 @@ class Auth_UserModel extends Unl_Model {
} }
static public static function findCurrentUser() static public function findCurrentUser()
{ {
$username = Zend_Auth::getInstance()->getIdentity(); $username = Zend_Auth::getInstance()->getIdentity();
if (!$username) { if (!$username) {
...@@ -92,7 +96,62 @@ class Auth_UserModel extends Unl_Model { ...@@ -92,7 +96,62 @@ class Auth_UserModel extends Unl_Model {
return self::findByUsername($username); return self::findByUsername($username);
} }
public function fetchNew() /**
* Returns a collection of the users that are members of the given group(s).
*
* @param Auth_GroupModel|Unl_Model_Collection $group
* @param bool[optional] $impliedMemberships
* @return Unl_Model_Collection
*/
static public function findByParentGroup($group, $impliedMemberships = true)
{
$db = Zend_Registry::get('db');
$select = new Zend_Db_Select($db);
$select->from(array('u' => 'creqUsers'), array('userId'));
$select->join(array('m' => 'creqGroupImpliedMemberships'), 'u.primaryGroup = m.childGroup', array('parentGroup'));
$select->where('m.type = 1');
if (!$impliedMemberships) {
$select->where("m.explicit = 'yes'");
}
if (Unl_Util::isArray($group)) {
if (count($group) == 0) {
return new Unl_Model_Collection(__CLASS__);
}
$select->where('parentGroup IN(?)', $group->getId());
} else {
$select->where('parentGroup = ?', $group->getId());
}
$records = $select->query()->fetchAll();
$userIds = array();
foreach ($records as $record) {
$userIds[] = $record['userId'];
}
self::find($userIds);
$users = array();
if (Unl_Util::isArray($group)) {
foreach ($group->getId() as $groupId) {
$users[$groupId] = new Unl_Model_Collection(__CLASS__);
}
} else {
$users[$group->getId()] = new Unl_Model_Collection(__CLASS__);
}
foreach ($records as $record) {
$users[$record['parentGroup']][$record['userId']] = Unl_Model_Registry::getInstance()->get(__CLASS__, $record['userId']);
}
if (Unl_Util::isArray($group)) {
return $users;
} else {
return $users[$group->getId()];
}
}
static public function fetchNew()
{ {
$data = array( $data = array(
'userId' => null, 'userId' => null,
...@@ -237,8 +296,8 @@ class Auth_UserModel extends Unl_Model { ...@@ -237,8 +296,8 @@ class Auth_UserModel extends Unl_Model {
$db->query($sql); $db->query($sql);
$db->query('DROP TABLE creqUsersUpdate'); $db->query('DROP TABLE creqUsersUpdate');
$sql = 'CREATE TEMPORARY TABLE creqUsersUpdate ' $sql = 'CREATE TEMPORARY TABLE creqPeopleUpdate '
. 'SELECT * FROM creqUsers LIMIT 0'; . 'SELECT * FROM creqPeople LIMIT 0';
$db->query($sql); $db->query($sql);
...@@ -327,6 +386,7 @@ class Auth_UserModel extends Unl_Model { ...@@ -327,6 +386,7 @@ class Auth_UserModel extends Unl_Model {
$sql .= implode(', ', $sqlParts); $sql .= implode(', ', $sqlParts);
$db->query($sql); $db->query($sql);
} }
public function getId() public function getId()
{ {
return $this->_data['userId']; return $this->_data['userId'];
...@@ -350,6 +410,15 @@ class Auth_UserModel extends Unl_Model { ...@@ -350,6 +410,15 @@ class Auth_UserModel extends Unl_Model {
$this->_data['firstName'] = $firstName; $this->_data['firstName'] = $firstName;
} }
public function getMiddleName()
{
return $this->_data['middleName'];
}
public function setMiddleName($middleName)
{
$this->_data['middleName'] = $middleName;
}
public function getLastName() public function getLastName()
{ {
return $this->_data['lastName']; return $this->_data['lastName'];
......
<h1>Editing Group: <?php echo $this->group->getName(); ?></h1>
<form action="<?php echo $this->baseUrl(); ?>/auth/user-admin/edit-group.post/id/<?php echo $this->group->getId(); ?>" method="post">
Name:<br />
<?php echo $this->formText('name', $this->group->getName()); ?><br />
Description:<br />
<?php echo $this->formText('description', $this->group->getDescription()); ?><br />
<br />Parent Groups:<br />
<?php echo $this->formSelect('parentGroups', $this->parentGroups, array('multiple' => 'multiple'), $this->groups) ?><br />
<br />Child Groups:<br />
<?php echo $this->formSelect('childGroups', $this->childGroups, array('multiple' => 'multiple'), $this->groups) ?><br />
<br />Users:<br />
<?php echo $this->formSelect('users', $this->userMembers, array('multiple' => 'multiple'), $this->users) ?><br />
<br />
<?php echo $this->formSubmit('submit', 'Update Group'); ?><br />
</form>
\ No newline at end of file
<h1>Editing User: <?php echo $this->user->getUsername(); ?></h1>
<form action="<?php echo $this->baseUrl(); ?>/auth/user-admin/edit-user.post/id/<?php echo $this->user->getId(); ?>" method="post">
First Name:<br />
<?php echo $this->formText('firstName', $this->user->getFirstName()); ?><br />
Middle Name:<br />
<?php echo $this->formText('middleName', $this->user->getMiddleName()); ?><br />
Last Name:<br />
<?php echo $this->formText('lastName', $this->user->getLastName()); ?><br />
Email:<br />
<?php echo $this->formText('email', $this->user->getEmail()); ?><br />
Phone:<br />
<?php echo $this->formText('phone', $this->user->getPhone()); ?><br />
Groups:<br />
<?php echo $this->formSelect('groups', $this->userGroups, array('multiple' => 'multiple'), $this->groups) ?><br />
<br />
<?php echo $this->formSubmit('submit', 'Update User'); ?><br />
</form>
\ No newline at end of file
<?php function displayGroup($group, $groupChildGroups, $groupUsers, $baseUrl) { ?>
<h3>
<a href="<?php echo $baseUrl; ?>/auth/user-admin/edit-group/id/<?php echo $group->getId(); ?>">
<?php echo $group->getName(); ?>
</a>
</h3>
<ul>
<?php foreach ($groupChildGroups[$group->getId()] as $childGroup) { ?>
<li>
<?php echo displayGroup($childGroup, $groupChildGroups, $groupUsers, $baseUrl) ?>
</li>
<?php } ?>
<?php foreach ($groupUsers[$group->getId()] as $user) { ?>
<li>
<a href="<?php echo $baseUrl; ?>/auth/user-admin/edit-user/id/<?php echo $user->getId(); ?>">
<?php echo $user->getUsername(); ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php $this->users->orderBy('getUsername'); ?>
<?php $this->groups->orderBy('getName'); ?>
<h1>Select a User or Group</h1>
<ul>
<?php foreach ($this->groups as $group) { ?>
<?php if (count($this->groupParentGroups[$group->getId()]) == 0) { ?>
<li>
<?php echo displayGroup($group, $this->groupChildGroups, $this->groupUsers, $this->baseUrl()); ?>
</li>
<?php } } ?>
</ul>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment