11 namespace Fossology\Lib\Dao;
16 use Symfony\Component\HttpFoundation\Session\Session;
24 const USER_ACTIVE_STATUS =
'active';
26 const SUPER_USER =
'fossy';
35 function __construct(
DbManager $dbManager, Logger $logger)
38 $this->logger = $logger;
41 $this->session = $container->get(
'session');
49 if (empty($groupId)) {
52 $userChoices = array();
53 $statementN = __METHOD__;
54 $sql =
"SELECT user_pk, user_name, user_desc FROM users LEFT JOIN group_user_member AS gum ON users.user_pk = gum.user_fk"
55 .
" WHERE gum.group_fk = $1 AND users.user_status='active'";
56 $this->
dbManager->prepare($statementN, $sql);
57 $res = $this->
dbManager->execute($statementN, array($groupId));
58 while ($rw = $this->
dbManager->fetchArray($res)) {
59 $userChoices[$rw[
'user_pk']] = $rw[
'user_desc'] .
' (' . $rw[
'user_name'] .
')';
73 return $this->
dbManager->createMap(
'groups',
'group_pk',
'group_name');
75 $sql =
"SELECT group_pk, group_name FROM groups, group_user_member"
76 .
" WHERE group_pk=group_fk AND user_fk=$1 AND group_perm=$2";
77 $param = array($userId,self::ADMIN);
78 $this->
dbManager->prepare($stmt=__METHOD__, $sql);
79 $res = $this->
dbManager->execute($stmt,$param);
81 while ($row = $this->
dbManager->fetchArray($res)) {
82 $groupMap[$row[
'group_pk']] = $row[
'group_name'];
95 $sql =
"SELECT group_pk, group_name FROM groups, group_user_member WHERE group_pk=group_fk AND user_fk=$1";
96 $this->
dbManager->prepare($stmt=__METHOD__, $sql);
97 $res = $this->
dbManager->execute($stmt,array($userId));
99 while ($row = $this->
dbManager->fetchArray($res)) {
100 $groupMap[$row[
'group_pk']] = $row[
'group_name'];
114 $sql =
"SELECT group_pk, group_name FROM groups LEFT JOIN users ON group_name=user_name "
115 .
"WHERE user_name IS NULL";
118 $sql =
"SELECT group_pk, group_name FROM groups LEFT JOIN users ON group_name=user_name "
119 .
" INNER JOIN group_user_member ON group_pk=group_user_member.group_fk AND user_fk=$1 AND group_perm=$2 "
120 .
"WHERE user_name IS NULL";
121 $param = array($userId,1);
123 $this->
dbManager->prepare($stmt=__METHOD__.
".$userLevel", $sql);
124 $res = $this->
dbManager->execute($stmt,$param);
126 while ($row = $this->
dbManager->fetchArray($res)) {
127 $groupMap[$row[
'group_pk']] = $row[
'group_name'];
142 if (!$this->session->isStarted()) {
143 $this->session->setName(
'Login');
144 $this->session->start();
146 $groupArray = $this->
dbManager->getSingleRow(
'SELECT group_pk, group_name FROM groups WHERE group_pk=$1',
147 array($groupId),__METHOD__.
'.exists');
148 if ($groupArray===
false) {
149 throw new \Exception( _(
"Group does not exist. Not deleted.") );
151 $groupConstraint = $this->
dbManager->getSingleRow(
'SELECT count(*) cnt FROM users WHERE user_name=$1',
152 array($groupArray[
'group_name']),__METHOD__.
'.contraint');
153 if ($groupConstraint[
'cnt']) {
154 throw new \Exception( _(
"Group must not be deleted due to name constraint.") );
158 $adminLevel = $this->
dbManager->getSingleRow(
"SELECT count(*) cnt FROM group_user_member WHERE group_fk=$1 and user_fk=$2 and group_perm=1",
159 array($groupId,$userId),__METHOD__.
'.admin_lvl');
160 if ($adminLevel[
'cnt']< 1) {
161 $text = _(
"Permission Denied.");
162 throw new \Exception($text);
167 $this->
dbManager->getSingleRow(
"DELETE FROM perm_upload WHERE group_fk=$1",array($groupId),__METHOD__.
'.perm_upload');
168 $this->
dbManager->getSingleRow(
"DELETE FROM group_user_member WHERE group_fk=$1",array($groupId),__METHOD__.
'.gum');
169 $this->
dbManager->getSingleRow(
"UPDATE users SET new_upload_group_fk=NULL, new_upload_perm=NULL WHERE new_upload_group_fk=$1",
170 array($groupId),__METHOD__.
'.upload_group');
171 $newGroupIdStmt =
'(SELECT group_fk FROM group_user_member WHERE user_fk=user_pk LIMIT 1)';
172 $this->
dbManager->getSingleRow(
"UPDATE users SET group_fk=$newGroupIdStmt WHERE group_fk=$1",
173 array($groupId),__METHOD__.
'.active_group');
174 $this->
dbManager->getSingleRow(
"DELETE FROM groups WHERE group_pk=$1",array($groupId),__METHOD__.
'.delete');
177 $newGroupId= $this->
dbManager->getSingleRow(
"SELECT group_fk FROM users WHERE user_pk=$1",
178 array($this->session->get(AUTH::USER_ID)), __METHOD__.
'.group_after_update');
185 function updateUserTable()
187 $statementBasename = __FUNCTION__;
190 $this->
dbManager->getSingleRow(
"UPDATE users SET user_perm = $1 WHERE user_perm IS NULL;",
192 $statementBasename .
'.setDefaultPermission');
196 if (empty($defaultUser[
'user_name'])) {
199 INSERT INTO users (user_name,user_desc,user_seed,user_pass,user_perm,user_email,root_folder_fk)
200 VALUES ('Default User','Default User when nobody is logged in','Seed','Pass', $1,NULL,1);",
201 array($level), $statementBasename .
'.createDefaultUser');
207 if (empty($row[
'user_name'])) {
209 $options = array(
'cost' => 10);
210 $hash = password_hash(self::SUPER_USER, PASSWORD_DEFAULT, $options);
213 if (empty($row0[
'user_name'])) {
215 INSERT INTO users (user_name, user_desc, user_seed, user_pass, user_perm, user_email, email_notify, root_folder_fk)
216 VALUES ($1,'Default Administrator',$2, $3, $4, $1,'y',1)",
217 array(self::SUPER_USER,
'Seed', $hash, $perm), $statementBasename .
'.createDefaultAdmin');
219 $this->
dbManager->getSingleRow(
"UPDATE users SET user_perm = $1, email_notify = 'y'," .
220 " user_email=$2 WHERE user_name =$2",
221 array($perm, self::SUPER_USER), $statementBasename .
'.updateDefaultUserToDefaultAdmin');
226 return empty($row[
'user_name']) ? 1 : 0;
235 return $this->
dbManager->getSingleRow(
"SELECT * FROM users WHERE user_name = $1", array($userName), __FUNCTION__);
244 return $this->
dbManager->getSingleRow(
"SELECT * FROM users WHERE user_pk = $1", array($userPk), __FUNCTION__);
253 $row = $this->
dbManager->getSingleRow(
"SELECT * FROM groups WHERE group_name = $1", array($groupName), __FUNCTION__);
254 return $row[
'group_pk'];
263 return $this->
dbManager->getSingleRow(
"SELECT * FROM users WHERE user_perm = $1", array($permission), __FUNCTION__);
272 $this->
dbManager->getSingleRow(
"UPDATE users SET group_fk=$2 WHERE user_pk=$1",
273 array($userId, $groupId), __FUNCTION__);
276 public function getUserAndDefaultGroupByUserName(&$userName, $oauth=
false)
279 $statement = __METHOD__;
281 $searchEmail =
" OR user_email=$1";
282 $statement .=
"oauth";
284 $userRow = $this->
dbManager->getSingleRow(
285 "SELECT users.*,group_name FROM users LEFT JOIN groups ON group_fk=group_pk WHERE user_name=$1$searchEmail",
286 array($userName), $statement);
287 if (empty($userRow)) {
288 throw new \Exception(
'invalid user name');
291 $userName = $userRow[
'user_name'];
293 $userRow[
'oauth'] = $oauth;
294 if ($userRow[
'group_fk']) {
299 $userRow[
'group_fk'] = $groupRow[
'group_fk'];
300 $userRow[
'group_name'] = $groupRow[
'group_name'];
310 $row = $this->
dbManager->getSingleRow(
"SELECT user_status FROM users WHERE user_name=$1",
311 array($userName), __METHOD__);
312 return $row!==
false && ($row[
'user_status']==self::USER_ACTIVE_STATUS);
321 $row = $this->
dbManager->getSingleRow(
"SELECT user_status FROM users WHERE user_pk=$1",
322 array($userId), __METHOD__);
323 return $row!==
false && ($row[
'user_status']==self::USER_ACTIVE_STATUS);
331 $this->
dbManager->getSingleRow(
"UPDATE users SET last_connection=now() WHERE user_pk=$1",
332 array($userId), __FUNCTION__);
342 $groupRow = $this->
dbManager->getSingleRow(
343 "SELECT group_fk,group_name FROM group_user_member LEFT JOIN groups ON group_fk=group_pk WHERE user_fk=$1",
344 array($userId), __FUNCTION__.
".getGroup");
350 if (empty($groupId)) {
351 $groupId = $this->
addGroup($groupName);
352 $this->addGroupMembership($groupId, $userId);
355 return array(
'group_fk'=>$groupId,
'group_name'=>$groupName);
358 public function isAdvisorOrAdmin($userId, $groupId)
360 $row = $this->
dbManager->getSingleRow(
"SELECT group_perm FROM group_user_member WHERE user_fk=$1 AND group_fk=$2",
361 array($userId, $groupId), __METHOD__);
362 return $row!==
false && ($row[
'group_perm']==self::ADVISOR || $row[
'group_perm']==self::ADMIN);
372 if (empty($groupName)) {
373 throw new \Exception(_(
"Error: Group name must be specified."));
376 $groupAlreadyExists = $this->
dbManager->getSingleRow(
"SELECT group_pk, group_name FROM groups WHERE LOWER(group_name)=LOWER($1)",
378 __METHOD__.
'.gExists');
379 if ($groupAlreadyExists) {
380 throw new \Exception(_(
"Group exists. Try different Name, Group-Name checking is case-insensitive and Duplicate not allowed"));
383 $this->
dbManager->insertTableRow(
'groups', array(
'group_name'=>$groupName));
384 $groupNowExists = $this->
dbManager->getSingleRow(
"SELECT * FROM groups WHERE group_name=$1",
386 __METHOD__.
'.gNowExists');
387 if (!$groupNowExists) {
388 throw new \Exception(_(
"Failed to create group"));
390 return $groupNowExists[
'group_pk'];
393 public function addGroupMembership($groupId, $userId, $groupPerm=1)
395 $this->
dbManager->insertTableRow(
'group_user_member',
396 array(
'group_fk'=>$groupId,
'user_fk'=>$userId,
'group_perm'=>$groupPerm));
405 $userRow = $this->
dbManager->getSingleRow(
"SELECT user_name FROM users WHERE user_pk=$1",array($userId),__METHOD__);
407 throw new \Exception(
'unknown user with id='.$userId);
409 return $userRow[
'user_name'];
418 $groupRow = $this->
dbManager->getSingleRow(
"SELECT group_name FROM groups WHERE group_pk = $1",array($groupId),__METHOD__);
419 if (empty($groupRow)) {
420 throw new \Exception(
'Error: GroupId ='. $groupId .
' not a member of a valid group.');
422 return $groupRow[
'group_name'];
431 $userRow = $this->
dbManager->getSingleRow(
"SELECT user_email FROM users WHERE user_pk=$1",array($userId),__METHOD__);
433 throw new \Exception(
'unknown user with id='.$userId);
435 return $userRow[
'user_email'];
444 return $this->
dbManager->getRows(
"SELECT * FROM users ORDER BY user_name;");
Contains the constants and helpers for authentication of user.
static getUserId()
Get the current user's id.
static getGroupId()
Get the current user's group id.
getGroupIdByName($groupName)
setDefaultGroupMembership($userId, $groupId)
fixDefaultGroup($userId, $groupName)
getUserChoices($groupId=null)
getDeletableAdminGroupMap($userId, $userLevel=0)
get array of groups that this user has admin access to
getUserGroupMap($userId)
get array of groups that this user has admin access to
deleteGroup($groupId)
Delete a group (for constraint, see http://www.fossology.org/projects/fossology/wiki/GroupsPerms )
updateUserLastConnection($userId)
getAdminGroupMap($userId, $userLevel=0)
get array of groups that this user has admin access to
getUserByPermission($permission)
getGroupNameById($groupId)
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
fo_dbManager * dbManager
fo_dbManager object