34 var $groupPermissions = array(-1 =>
"None", UserDao::USER =>
"User",
35 UserDao::ADMIN =>
"Admin", UserDao::ADVISOR =>
"Advisor");
36 const NAME =
'group_manage_users';
38 function __construct()
40 parent::__construct(self::NAME, array(
41 self::TITLE => _(
"Manage Group Users"),
42 self::MENU_LIST =>
"Admin::Groups::Manage Group Users",
44 self::REQUIRES_LOGIN => TRUE
52 protected function handle(Request $request)
57 $groupMap = $userDao->getAdminGroupMap($userId, $_SESSION[
Auth::USER_LEVEL]);
58 if (empty($groupMap)) {
59 $text = _(
"You have no permission to manage any group.");
60 return $this->
render(
'include/base.html.twig', $this->mergeWithDefault(array(
'message' => $text)));
63 $dbManager = $this->
getObject(
'db.manager');
64 $group_pk = intval($request->get(
'group'));
65 if (empty($group_pk) || !array_key_exists($group_pk, $groupMap)) {
66 $group_pk = key($groupMap);
69 $gum_pk = intval($request->get(
'gum_pk'));
72 $perm = intval($request->get(
'perm'));
73 $atleastOneUserShouldBePart = $dbManager->getSingleRow(
"SELECT count(*) cnt FROM group_user_member WHERE group_fk = (SELECT group_fk FROM group_user_member WHERE group_user_member_pk = $1)",
74 array($gum_pk), $stmt = __METHOD__ .
".atleastOneUserShouldBePart");
75 if ($atleastOneUserShouldBePart[
'cnt'] <= 1) {
76 $text = _(
"Error: atleast one user should be part of a group.");
78 $this->updateGUMPermission($gum_pk, $perm);
80 $groupMap = $userDao->getAdminGroupMap($userId,
81 $_SESSION[Auth::USER_LEVEL]);
84 $newuser = intval($request->get(
'newuser'));
85 $newperm = intval($request->get(
'newperm'));
87 if ($newuser && $group_pk) {
89 $dbManager->prepare($stmt = __METHOD__ .
".delByGroupAndUser",
90 "delete from group_user_member where group_fk=$1 and user_fk=$2");
91 $dbManager->freeResult(
92 $dbManager->execute($stmt, array($group_pk, $newuser)));
94 $dbManager->prepare($stmt = __METHOD__ .
".insertGUP",
95 "insert into group_user_member (group_fk, user_fk, group_perm) values ($1,$2,$3)");
96 $dbManager->freeResult(
97 $dbManager->execute($stmt, array($group_pk, $newuser, $newperm)));
99 if ($newuser == $userId) {
100 $groupMap = $userDao->getAdminGroupMap($userId, $_SESSION[Auth::USER_LEVEL]);
102 $newperm = $newuser = 0;
105 natcasesort($groupMap);
107 $onchange =
"onchange=\"js_url(this.value, '$baseUrl')\"";
108 $baseUrl .= $group_pk;
109 $vars = array(
'groupMap' => $groupMap,
110 'groupId' => $group_pk,
111 'permissionMap' => $this->groupPermissions,
112 'baseUrl' => $baseUrl,
113 'groupMapAction' => $onchange);
115 $stmt = __METHOD__ .
"getUsersWithGroup";
116 $dbManager->prepare($stmt,
"select user_pk, user_name, user_desc, group_user_member_pk, group_perm 117 FROM users LEFT JOIN group_user_member gum ON gum.user_fk=users.user_pk AND gum.group_fk=$1 118 ORDER BY user_name");
119 $result = $dbManager->execute($stmt, array($group_pk));
120 $vars[
'usersWithGroup'] = $dbManager->fetchAll($result);
121 $dbManager->freeResult($result);
123 $otherUsers = array(
'0' =>
'');
124 foreach ($vars[
'usersWithGroup'] as $row) {
125 if ($row[
'group_user_member_pk']) {
128 $otherUsers[$row[
'user_pk']] = !empty($row[
'user_desc']) ? $row[
'user_desc'].
' ('. $row[
'user_name'] .
')' : $row[
'user_name'];
131 $vars[
'existsOtherUsers'] = count($otherUsers) - 1;
132 if ($vars[
'existsOtherUsers']) {
133 $vars[
'newPermissionMap'] = $this->groupPermissions;
134 unset($vars[
'newPermissionMap'][-1]);
135 $script =
"var newpermurl; 136 function setNewPermUrl(newperm){ 137 newpermurl='" . $baseUrl .
"&newperm='+newperm+'&newuser='; 139 setNewPermUrl($newperm);";
140 $scripts =
js_url() .
'<script type="text/javascript"> ' . $script .
'</script>';
141 $vars[
'otherUsers'] = $otherUsers;
146 $vars[
'scripts'] = $scripts;
148 $vars[
'message'] .= $text;
150 return $this->
render(
'admin_group_users.html.twig', $this->mergeWithDefault($vars));
153 private function updateGUMPermission($gum_pk, $perm)
155 $dbManager = $this->
getObject(
'db.manager');
157 $dbManager->prepare($stmt = __METHOD__ .
".delByGUM",
158 "DELETE FROM group_user_member WHERE group_user_member_pk=$1 RETURNING user_fk, group_fk");
159 $deletedEntry = $dbManager->execute($stmt, array($gum_pk));
160 $effectedUser = $dbManager->fetchArray($deletedEntry);
161 $isEffected = $dbManager->getSingleRow(
"SELECT count(*) cnt FROM users WHERE user_pk=$1 AND group_fk = $2",
162 array($effectedUser[
'user_fk'], $effectedUser[
'group_fk']), $stmt = __METHOD__ .
".isUserEffectedFromRemoval");
163 if ($isEffected[
'cnt'] == 1) {
164 $dbManager->getSingleRow(
"UPDATE users SET group_fk = ( 165 SELECT group_fk FROM group_user_member WHERE user_fk = $1 AND group_perm >= 0 LIMIT 1) 167 array($effectedUser[
'user_fk']), $stmt = __METHOD__ .
".setNewGroupId");
169 $dbManager->freeResult($deletedEntry);
170 }
else if (array_key_exists($perm, $this->groupPermissions)) {
171 $dbManager->getSingleRow(
"UPDATE group_user_member SET group_perm=$1 WHERE group_user_member_pk=$2",
172 array($perm, $gum_pk), $stmt = __METHOD__ .
".updatePermInGUM");
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user's id.
render($templateName, $vars=null, $headers=null)