FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fo_usergroup.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2015 Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ***********************************************************/
18 
19 
23 
24 require_once("$MODDIR/lib/php/common-cli.php");
25 cli_Init();
26 require_once("$MODDIR/lib/php/common-users.php");
27 
28 error_reporting(E_ALL);
29 
30 $usage = "Usage: " . basename($argv[0]) . " [options]
31  --username = admin/user with user-creation permissions
32  --password = admin/user password
33  --uname = username to create if not exists
34  --gname = groupname to create if not exists
35  --upasswd = password of created user
36  --permlvl = group permission level (-1: None, ".UserDao::USER.": User, ".UserDao::ADMIN.": Admin, ".UserDao::ADVISOR.": Advisor)
37  --accesslvl = user database permission level (".Auth::PERM_NONE.": None, ".Auth::PERM_READ.": Read, ".Auth::PERM_WRITE.": Write, ".Auth::PERM_ADMIN.": Admin)
38  --folder = root folder
39 ";
40 $opts = getopt("h", array('username:', 'password:', 'uname:', 'gname:', 'upasswd:', 'permlvl:', 'accesslvl:', 'folder:'));
41 
42 if (array_key_exists('h',$opts)) {
43  print "$usage\n";
44  return 0;
45 }
46 
47 $adminName = array_key_exists("username", $opts) ? $opts["username"] : null;
48 $passwd = array_key_exists("password", $opts) ? $opts["password"] : null;
49 if (!account_check($adminName, $passwd, $group)) {
50  print "Fossology login failure\n";
51  return 2;
52 } else {
53  print "Logged in as user $adminName\n";
54 }
55 
57 $userDao = $GLOBALS['container']->get("dao.user");
59 $folderDao = $GLOBALS['container']->get("dao.folder");
60 
61 $adminRow = $userDao->getUserByName($adminName);
62 if ($adminRow["user_perm"] < PLUGIN_DB_ADMIN) {
63  print "You have no permission to admin the user group thingy\n";
64  return 1;
65 }
66 
67 $uName = array_key_exists("uname", $opts) ? $opts["uname"] : '';
68 $user = $uName ? $userDao->getUserByName($uName) : false;
69 if ($user !== false) {
70  print "The user already exists, and updates in permissions not done from the commandline, we will only add group rights\n";
71 }
72 
73 if ($uName && !$user) {
74  $pass = array_key_exists('upasswd', $opts) ? $opts['upasswd'] : '';
75  $seed = rand() . rand();
76  $hash = sha1($seed . $pass);
77  $desc = 'created via cli';
78  $perm = array_key_exists('accesslvl', $opts) ? intval($opts['accesslvl']) : 0;
79  if (array_key_exists('folder', $opts)) {
80  $folder = $opts['folder'];
81  $folderid = $folderDao->getFolderId($folder);
82 
83  if ($folderid == null) {
84  $folderid = $folderDao->insertFolder($folder, 'Cli generated folder');
85  }
86 
87  } else {
88  $folderid=1;
89  }
90  $agentList = userAgents();
91  $email = $emailNotify = '';
92  add_user($uName, $desc, $seed, $hash, $perm, $email, $emailNotify, $agentList, $folderid);
93  $user = $userDao->getUserByName($uName);
94  print "added user $uName\n";
95 }
96 
97 $gName = array_key_exists("gname", $opts) ? $opts["gname"] : '';
98 if ($gName) {
99  $sql = "SELECT group_pk FROM groups WHERE group_name=$1";
100  $groupRow = $dbManager->getSingleRow($sql, array($gName), __FILE__ . __LINE__);
101  $groupId = $groupRow ? $groupRow['group_pk'] : $userDao->addGroup($gName);
102 } else {
103  $groupId = false;
104 }
105 
106 $permLvl = array_key_exists("permlvl", $opts) ? intval($opts["permlvl"]) : 0;
107 if ($user && $groupId) {
108  $sql = "SELECT group_user_member_pk id FROM group_user_member WHERE user_fk=$1 AND group_fk=$2";
109  $gumRow = $dbManager->getSingleRow($sql,array($user['user_pk'],$groupId),__FILE__.__LINE__);
110 }
111 
112 if ($user && $groupId && $permLvl<0 && $gumRow) {
113  $dbManager->prepare($stmt = __FILE__.__LINE__,
114  "delete from group_user_member where group_user_member_pk=$1");
115  $dbManager->freeResult($dbManager->execute($stmt, array($gumRow['id'])));
116  print "deleted membership of $uName in $gName\n";
117 } else if ($user && $groupId && $permLvl>=0 && $gumRow) {
118  $dbManager->getSingleRow("update group_user_member set group_perm=$1 where group_user_member_pk=$2",
119  array($permLvl, $gumRow['id']), __FILE__.__LINE__);
120  print "update membership of $uName in $gName\n";
121 } else if ($user && $groupId && $permLvl>=0) {
122  $dbManager->insertTableRow('group_user_member',
123  array('group_perm'=>$permLvl,'user_fk'=>$user['user_pk'],'group_fk'=>$groupId));
124  print "inserted membership of $uName in $gName\n";
125 } else {
126  print ".\n";
127 }
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:87
userAgents()
Read the UI form and format the user selected agents into a comma separated list. ...
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
#define PERM_NONE
User has no permission (not logged in)
Definition: libfossology.h:43
add_user($User, $Desc, $Seed, $Hash, $Perm, $Email, $Email_notify, $agentList, $Folder, $default_bucketpool_fk='')
Add a user.
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:36
#define PERM_READ
Read-only permission.
Definition: libfossology.h:44
#define PERM_ADMIN
Administrator.
Definition: libfossology.h:46
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45