3 * FusionForge system users integration
5 * Copyright 2004, Christian Bayle
6 * Copyright 2010, Roland Mas
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 require_once $gfcommon.'include/System.class.php';
26 class pgsql extends System {
32 * Value to add to unix_uid to get unix uid
34 * @var constant $UID_ADD
39 * Value to add to group_id to get unix gid
41 * @var constant $GID_ADD
46 * Value to add to unix gid to get unix uid of anoncvs special user
48 * @var constant $SCM_UID_ADD
50 var $SCM_UID_ADD = 50000;
53 * pgsql() - CONSTRUCTOR
62 * sysUseUnixName() - Check if user/group used the unix_name
64 * @param string The unix_name to check
65 * @return boolean true if used/false is free
67 function sysUseUnixName($unix_name) {
68 $res1 = db_query_params('SELECT user_id FROM users
69 WHERE user_name=$1',array($unix_name));
70 $res2 = db_query_params('SELECT group_id FROM groups
71 WHERE unix_group_name=$1',array($unix_name));
72 if ( db_numrows($res1) == 0 && db_numrows($res2) == 0 ){
79 * User management functions
83 * sysCheckUser() - Check for the existence of a user
85 * @param int The user ID of the user to check
86 * @return boolean true on success/false on error
89 function sysCheckUser($user_id) {
90 $user =& user_get_object($user_id);
98 * sysCreateUser() - Create a user
100 * @param int The user ID of the user to create
101 * @return boolean success or not
104 function sysCreateUser($user_id) {
105 $user = &user_get_object($user_id);
109 $res = db_query_params ('UPDATE users SET
114 array ($this->UID_ADD,
119 $this->setError('ERROR - Could Not Update User UID/GID: '.db_error());
122 $res1 = db_query_params ('DELETE FROM nss_usergroups WHERE user_id=$1',
125 $this->setError('ERROR - Could Not Delete Group Member(s): '.db_error());
128 // This is group used for user, not a real project
129 $res2 = db_query_params ('DELETE FROM nss_groups WHERE name IN
130 (SELECT user_name FROM users WHERE user_id=$1)',
133 $this->setError('ERROR - Could Not Delete Group GID: '.db_error());
136 $res3 = db_query_params ('INSERT INTO nss_groups
137 (user_id, group_id,name, gid)
138 SELECT user_id, 0, user_name, unix_gid
139 FROM users WHERE user_id=$1',
142 $this->setError('ERROR - Could Not Update Group GID: '.db_error());
147 foreach ($user->getGroups() as $p) {
148 $pids[] = $p->getID() ;
150 foreach ($user->getRoles() as $r) {
151 foreach ($r->getLinkedProjects() as $p) {
152 if (forge_check_perm_for_user ($user, 'scm', $p->getID(), 'write')) {
153 $pids[] = $p->getID() ;
157 foreach (array_unique($pids) as $pid) {
158 $this->sysGroupAddUser($pid, $user_id) ;
165 * sysCheckCreateUser() - Check that a user has been created
167 * @param int The ID of the user to check
168 * @return boolean true on success/false on error
171 function sysCheckCreateUser($user_id) {
172 return $this->sysCreateUser($user_id);
176 * sysCheckCreateGroup() - Check that a group has been created
178 * @param int The ID of the user to check
179 * @return boolean true on success/false on error
182 function sysCheckCreateGroup($user_id) {
183 return $this->sysCreateGroup($user_id);
187 * sysRemoveUser() - Remove a user
189 * @param int The user ID of the user to remove
190 * @return boolean true on success/false on failure
193 function sysRemoveUser($user_id) {
194 $res = db_query_params('UPDATE users SET unix_status=$1 WHERE user_id=$2',
198 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
201 $res1 = db_query_params ('DELETE FROM nss_usergroups WHERE user_id=$1',
204 $this->setError('ERROR - Could Not Delete Group Member(s): '.db_error());
207 // This is group used for user, not a real project
208 $res2 = db_query_params ('DELETE FROM nss_groups WHERE name IN
209 (SELECT user_name FROM users WHERE user_id=$1)',
212 $this->setError('ERROR - Could Not Delete Group GID: '.db_error());
220 * sysUserSetAttribute() - Set an attribute for a user
222 * @param int The user ID
223 * @param string The attribute to set
224 * @param string The new value of the attribute
225 * @return boolean true on success/false on error
228 function sysUserSetAttribute($user_id,$attr,$value) {
233 * Group management functions
237 * sysCheckGroup() - Check for the existence of a group
239 * @param int The ID of the group to check
240 * @return boolean true on success/false on error
243 function sysCheckGroup($group_id) {
244 $group = group_get_object($group_id);
248 $res = db_query_params('SELECT group_id FROM nss_groups WHERE group_id=$1',
250 if (db_numrows($res) == 0){
259 * sysCreateGroup() - Create a group
261 * @param int The ID of the group to create
262 * @returns true on success/false on error
265 function sysCreateGroup($group_id) {
266 $group = group_get_object($group_id);
271 $res1 = db_query_params ('DELETE FROM nss_usergroups WHERE group_id=$1',
274 $this->setError('ERROR - Could Not Delete Group Member(s): '.db_error());
277 $res3 = db_query_params ('DELETE FROM nss_groups WHERE group_id=$1',
280 $this->setError('ERROR - Could Not Delete Group GID: '.db_error());
283 $res4 = db_query_params ('INSERT INTO nss_groups
284 (user_id, group_id, name, gid)
285 SELECT 0, group_id, unix_group_name, group_id + $1
288 array ($this->GID_ADD,
291 $this->setError('ERROR - Could Not Insert Group GID: '.db_error());
294 $res5 = db_query_params ('INSERT INTO nss_groups
295 (user_id, group_id, name, gid)
296 SELECT 0, group_id, $1 || unix_group_name, group_id + $2
304 $this->setError('ERROR - Could Not Insert SCM Group GID: '.db_error());
308 foreach ($group->getUsers() as $u) {
309 $this->sysGroupAddUser ($group_id, $u->getID()) ;
316 * sysRemoveGroup() - Remove a group
318 * @param int The ID of the group to remove
319 * @returns true on success/false on error
322 function sysRemoveGroup($group_id) {
323 $res1 = db_query_params ('DELETE FROM nss_usergroups WHERE group_id=$1',
326 $this->setError('ERROR - Could Not Delete Group Member(s): '.db_error());
329 $res3 = db_query_params ('DELETE FROM nss_groups WHERE group_id=$1',
332 $this->setError('ERROR - Could Not Delete Group GID: '.db_error());
339 * sysGroupAddUser() - Add a user to a group
341 * @param int The ID of the group two which the user will be added
342 * @param int The ID of the user to add
343 * @param bool ignored
344 * @return boolean true on success/false on error
346 function sysGroupAddUser($group_id,$user_id,$foo=NULL) {
347 return $this->sysGroupCheckUser($group_id,$user_id) ;
351 * sysGroupCheckUser() - Sync user's Unix permissions with their FF permissions within a group
353 * @param int The ID of the group
354 * @param int The ID of the user
355 * @return boolean true on success/false on error
357 function sysGroupCheckUser($group_id,$user_id) {
359 if (! $this->sysGroupRemoveUser($group_id,$user_id)) {
364 $u = user_get_object($user_id) ;
365 $p = group_get_object($group_id) ;
366 if (forge_check_perm_for_user($u,'scm',$group_id,'write')) {
367 $res = db_query_params ('INSERT INTO nss_usergroups (
368 SELECT users.unix_uid AS uid,
369 groups.group_id + $1 AS gid,
370 users.user_id AS user_id,
371 groups.group_id AS group_id,
372 users.user_name AS user_name,
373 $2 || groups.unix_group_name AS unix_group_name
375 WHERE users.user_id=$3
377 AND users.unix_status=$5
379 AND groups.group_id=$7)',
380 array($this->SCM_UID_ADD,
387 $this->setError('ERROR - Could Not Update Group Member(s): '.db_error());
392 if ($u->isMember($p)) {
393 $res = db_query_params ('INSERT INTO nss_usergroups (
394 SELECT users.unix_uid AS uid,
395 groups.group_id + $1 AS gid,
396 users.user_id AS user_id,
397 groups.group_id AS group_id,
398 users.user_name AS user_name,
399 groups.unix_group_name AS unix_group_name
401 WHERE users.user_id=$2
403 AND users.unix_status=$4
405 AND groups.group_id=$6)',
406 array ($this->GID_ADD,
411 $this->setError('ERROR - Could Not Update Group Member(s): '.db_error());
422 * sysGroupRemoveUser() - Remove a user from a group
424 * @param int The ID of the group from which to remove the user
425 * @param int The ID of the user to remove
426 * @param boolean Compatibility issue but not used : pgsql extends System...
427 * @return boolean true on success/false on error
429 function sysGroupRemoveUser($group_id, $user_id, $unused = 0) {
430 $res = db_query_params ('DELETE FROM nss_usergroups WHERE user_id=$1 AND group_id=$2',
434 $this->setError('ERROR - Could Not Delete Group Member(s): '.db_error());
443 // c-file-style: "bsd"