33 const SUPER_USER =
'fossy';
42 function __construct(
DbManager $dbManager, Logger $logger)
45 $this->logger = $logger;
48 $this->session = $container->get(
'session');
56 if (empty($groupId)) {
59 $userChoices = array();
60 $statementN = __METHOD__;
61 $sql =
"SELECT user_pk, user_name, user_desc FROM users LEFT JOIN group_user_member AS gum ON users.user_pk = gum.user_fk" 62 .
" WHERE gum.group_fk = $1";
63 $this->
dbManager->prepare($statementN, $sql);
64 $res = $this->
dbManager->execute($statementN, array($groupId));
65 while ($rw = $this->
dbManager->fetchArray($res)) {
66 $userChoices[$rw[
'user_pk']] = $rw[
'user_desc'] .
' (' . $rw[
'user_name'] .
')';
80 return $this->
dbManager->createMap(
'groups',
'group_pk',
'group_name');
82 $sql =
"SELECT group_pk, group_name FROM groups, group_user_member" 83 .
" WHERE group_pk=group_fk AND user_fk=$1 AND group_perm=$2";
84 $param = array($userId,self::ADMIN);
85 $this->
dbManager->prepare($stmt=__METHOD__, $sql);
86 $res = $this->
dbManager->execute($stmt,$param);
88 while ($row = $this->
dbManager->fetchArray($res)) {
89 $groupMap[$row[
'group_pk']] = $row[
'group_name'];
102 $sql =
"SELECT group_pk, group_name FROM groups, group_user_member WHERE group_pk=group_fk AND user_fk=$1";
103 $this->
dbManager->prepare($stmt=__METHOD__, $sql);
104 $res = $this->
dbManager->execute($stmt,array($userId));
106 while ($row = $this->
dbManager->fetchArray($res)) {
107 $groupMap[$row[
'group_pk']] = $row[
'group_name'];
121 $sql =
"SELECT group_pk, group_name FROM groups LEFT JOIN users ON group_name=user_name " 122 .
"WHERE user_name IS NULL";
125 $sql =
"SELECT group_pk, group_name FROM groups LEFT JOIN users ON group_name=user_name " 126 .
" INNER JOIN group_user_member ON group_pk=group_user_member.group_fk AND user_fk=$1 AND group_perm=$2 " 127 .
"WHERE user_name IS NULL";
128 $param = array($userId,1);
130 $this->
dbManager->prepare($stmt=__METHOD__.
".$userLevel", $sql);
131 $res = $this->
dbManager->execute($stmt,$param);
133 while ($row = $this->
dbManager->fetchArray($res)) {
134 $groupMap[$row[
'group_pk']] = $row[
'group_name'];
149 if (!$this->session->isStarted()) {
150 $this->session->setName(
'Login');
151 $this->session->start();
153 $groupArray = $this->
dbManager->getSingleRow(
'SELECT group_pk, group_name FROM groups WHERE group_pk=$1',
154 array($groupId),__METHOD__.
'.exists');
155 if ($groupArray===
false) {
156 throw new \Exception( _(
"Group does not exist. Not deleted.") );
158 $groupConstraint = $this->
dbManager->getSingleRow(
'SELECT count(*) cnt FROM users WHERE user_name=$1',
159 array($groupArray[
'group_name']),__METHOD__.
'.contraint');
160 if ($groupConstraint[
'cnt']) {
161 throw new \Exception( _(
"Group must not be deleted due to name constraint.") );
165 $adminLevel = $this->
dbManager->getSingleRow(
"SELECT count(*) cnt FROM group_user_member WHERE group_fk=$1 and user_fk=$2 and group_perm=1",
166 array($groupId,$userId),__METHOD__.
'.admin_lvl');
167 if ($adminLevel[
'cnt']< 1) {
168 $text = _(
"Permission Denied.");
169 throw new \Exception($text);
174 $this->
dbManager->getSingleRow(
"DELETE FROM perm_upload WHERE group_fk=$1",array($groupId),__METHOD__.
'.perm_upload');
175 $this->
dbManager->getSingleRow(
"DELETE FROM group_user_member WHERE group_fk=$1",array($groupId),__METHOD__.
'.gum');
176 $this->
dbManager->getSingleRow(
"UPDATE users SET new_upload_group_fk=NULL, new_upload_perm=NULL WHERE new_upload_group_fk=$1",
177 array($groupId),__METHOD__.
'.upload_group');
178 $newGroupIdStmt =
'(SELECT group_fk FROM group_user_member WHERE user_fk=user_pk LIMIT 1)';
179 $this->
dbManager->getSingleRow(
"UPDATE users SET group_fk=$newGroupIdStmt WHERE group_fk=$1",
180 array($groupId),__METHOD__.
'.active_group');
181 $this->
dbManager->getSingleRow(
"DELETE FROM groups WHERE group_pk=$1",array($groupId),__METHOD__.
'.delete');
184 $newGroupId= $this->
dbManager->getSingleRow(
"SELECT group_fk FROM users WHERE user_pk=$1",
185 array($this->session->get(AUTH::USER_ID)), __METHOD__.
'.group_after_update');
192 function updateUserTable()
194 $statementBasename = __FUNCTION__;
195 $this->
dbManager->getSingleRow(
"UPDATE users SET user_seed = $1 WHERE user_seed IS NULL;",
197 $statementBasename .
'.randomizeEmptySeeds');
200 $this->
dbManager->getSingleRow(
"UPDATE users SET user_perm = $1 WHERE user_perm IS NULL;",
202 $statementBasename .
'.setDefaultPermission');
206 if (empty($defaultUser[
'user_name'])) {
209 INSERT INTO users (user_name,user_desc,user_seed,user_pass,user_perm,user_email,root_folder_fk) 210 VALUES ('Default User','Default User when nobody is logged in','Seed','Pass', $1,NULL,1);",
211 array($level), $statementBasename .
'.createDefaultUser');
217 if (empty($row[
'user_name'])) {
219 $seed = rand() . rand();
220 $hash = sha1($seed . self::SUPER_USER);
223 if (empty($row0[
'user_name'])) {
225 INSERT INTO users (user_name, user_desc, user_seed, user_pass, user_perm, user_email, email_notify, root_folder_fk) 226 VALUES ($1,'Default Administrator',$2, $3, $4, $1,'y',1)",
227 array(self::SUPER_USER, $seed, $hash, $perm), $statementBasename .
'.createDefaultAdmin');
229 $this->
dbManager->getSingleRow(
"UPDATE users SET user_perm = $1, email_notify = 'y'," .
230 " user_email=$2 WHERE user_name =$2",
231 array($perm, self::SUPER_USER), $statementBasename .
'.updateDefaultUserToDefaultAdmin');
236 return empty($row[
'user_name']) ? 1 : 0;
245 return $this->
dbManager->getSingleRow(
"SELECT * FROM users WHERE user_name = $1", array($userName), __FUNCTION__);
254 return $this->
dbManager->getSingleRow(
"SELECT * FROM users WHERE user_pk = $1", array($userPk), __FUNCTION__);
263 $row = $this->
dbManager->getSingleRow(
"SELECT * FROM groups WHERE group_name = $1", array($groupName), __FUNCTION__);
264 return $row[
'group_pk'];
273 return $this->
dbManager->getSingleRow(
"SELECT * FROM users WHERE user_perm = $1", array($permission), __FUNCTION__);
282 $this->
dbManager->getSingleRow(
"UPDATE users SET group_fk=$2 WHERE user_pk=$1",
283 array($userId, $groupId), __FUNCTION__);
286 public function getUserAndDefaultGroupByUserName($userName)
288 $userRow = $this->
dbManager->getSingleRow(
289 "SELECT users.*,group_name FROM users LEFT JOIN groups ON group_fk=group_pk WHERE user_name=$1",
290 array($userName), __FUNCTION__);
291 if (empty($userRow)) {
292 throw new \Exception(
'invalid user name');
294 if ($userRow[
'group_fk']) {
299 $userRow[
'group_fk'] = $groupRow[
'group_fk'];
300 $userRow[
'group_name'] = $groupRow[
'group_name'];
311 $groupRow = $this->
dbManager->getSingleRow(
312 "SELECT group_fk,group_name FROM group_user_member LEFT JOIN groups ON group_fk=group_pk WHERE user_fk=$1",
313 array($userId), __FUNCTION__.
".getGroup");
319 if (empty($groupId)) {
320 $groupId = $this->
addGroup($groupName);
321 $this->addGroupMembership($groupId, $userId);
324 return array(
'group_fk'=>$groupId,
'group_name'=>$groupName);
327 public function isAdvisorOrAdmin($userId, $groupId)
329 $row = $this->
dbManager->getSingleRow(
"SELECT group_perm FROM group_user_member WHERE user_fk=$1 AND group_fk=$2",
330 array($userId, $groupId), __METHOD__);
331 return $row!==
false && ($row[
'group_perm']==self::ADVISOR || $row[
'group_perm']==self::ADMIN);
341 if (empty($groupName)) {
342 throw new \Exception(_(
"Error: Group name must be specified."));
345 $groupAlreadyExists = $this->
dbManager->getSingleRow(
"SELECT group_pk FROM groups WHERE group_name=$1",
347 __METHOD__.
'.gExists');
348 if ($groupAlreadyExists) {
349 throw new \Exception(_(
"Group already exists. Not added."));
352 $this->
dbManager->insertTableRow(
'groups', array(
'group_name'=>$groupName));
353 $groupNowExists = $this->
dbManager->getSingleRow(
"SELECT * FROM groups WHERE group_name=$1",
355 __METHOD__.
'.gNowExists');
356 if (!$groupNowExists) {
357 throw new \Exception(_(
"Failed to create group"));
359 return $groupNowExists[
'group_pk'];
362 public function addGroupMembership($groupId, $userId)
364 $this->
dbManager->insertTableRow(
'group_user_member',
365 array(
'group_fk'=>$groupId,
'user_fk'=>$userId,
'group_perm'=>1));
374 $userRow = $this->
dbManager->getSingleRow(
"SELECT user_name FROM users WHERE user_pk=$1",array($userId),__METHOD__);
376 throw new \Exception(
'unknown user with id='.$userId);
378 return $userRow[
'user_name'];
387 $groupRow = $this->
dbManager->getSingleRow(
"SELECT group_name FROM groups WHERE group_pk = $1",array($groupId),__METHOD__);
388 if (empty($groupRow)) {
389 throw new \Exception(
'Error: GroupId ='. $groupId .
' not a member of a valid group.');
391 return $groupRow[
'group_name'];
400 $userRow = $this->
dbManager->getSingleRow(
"SELECT user_email FROM users WHERE user_pk=$1",array($userId),__METHOD__);
402 throw new \Exception(
'unknown user with id='.$userId);
404 return $userRow[
'user_email'];
getUserGroupMap($userId)
get array of groups that this user has admin access to
static getUserId()
Get the current user's id.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
getAdminGroupMap($userId, $userLevel=0)
get array of groups that this user has admin access to
getUserChoices($groupId=null)
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
getUserByPermission($permission)
setDefaultGroupMembership($userId, $groupId)
getGroupIdByName($groupName)
getGroupNameById($groupId)
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
fo_dbManager * dbManager
fo_dbManager object
deleteGroup($groupId)
Delete a group (for constraint, see http://www.fossology.org/projects/fossology/wiki/GroupsPerms ) ...
getDeletableAdminGroupMap($userId, $userLevel=0)
get array of groups that this user has admin access to
static getGroupId()
Get the current user's group id.
fixDefaultGroup($userId, $groupName)