17 include_once (
'Class.DbObj.php');
18 include_once (
'Class.QueryDb.php');
19 include_once (
'Class.Log.php');
20 include_once (
'Class.Application.php');
21 include_once (
'Class.Group.php');
22 include_once (
'WHAT/Lib.Common.php');
25 define(
"ANONYMOUS_ID", 3);
26 define(
"GADMIN_ID", 4);
107 create table users ( id int not null,
112 password text not null,
122 create index users_idx2 on users(lastname);
123 CREATE UNIQUE INDEX users_login on users (login);
124 create sequence seq_id_users start 10;";
135 $query->AddQuery(
"login='" . pg_escape_string(
$login) .
"'");
139 $this->Affect(
$list[0]);
152 if ($this->substitute) {
153 return new Account($this->dbaccess, $this->substitute);
165 $incumbents = array();
167 $sql = sprintf(
"select %s from users where substitute=%d;", $returnSystemIds ?
'id' :
'fid', $this->
id);
182 $err = sprintf(_(
"cannot set substitute account object not affected"));
187 $sql = sprintf(
"select id from users where login = '%s'", pg_escape_string(
$substitute));
190 else $err = sprintf(_(
"cannot set substitute %s login not found") ,
$substitute);
195 if (!$substituteId)
$err = sprintf(_(
"cannot set substitute %s id not found") ,
$substitute);
199 $err = sprintf(_(
"cannot substitute itself"));
207 $u = new \Account($this->dbaccess, $this->substitute);
208 $u->updateMemberOf();
209 if ($oldSubstitute) {
210 $u->select($oldSubstitute);
211 $u->updateMemberOf();
215 if ($action->user->id == $u->id) $action->user->revert();
242 $this->Affect(
$list[0]);
252 if ((!$this->login) && $this->accounttype == self::ROLE_TYPE) {
254 $this->login = uniqid(
'role');
257 if ($this->setloginName($this->login))
return _(
"this login exists");
258 if ($this->login ==
"")
return _(
"login must not be empty");
259 $this->login = mb_strtolower($this->login);
260 if ($this->
id ==
"") {
261 $res = pg_query($this->dbid,
"select nextval ('seq_id_users')");
262 $arr = pg_fetch_array(
$res, 0);
263 $this->
id = $arr[
"nextval"];
266 if (($this->accounttype == self::GROUP_TYPE) || ($this->accounttype == self::ROLE_TYPE) || ($this->isgroup ==
"Y")) {
267 if ((!$this->accounttype) && ($this->isgroup ==
"Y")) $this->accounttype = self::GROUP_TYPE;
268 $this->password =
'-';
269 if ($this->accounttype === self::GROUP_TYPE) {
270 $this->isgroup =
"Y";
273 $this->isgroup =
"N";
275 if (!$this->accounttype) {
276 $this->accounttype = self::USER_TYPE;
279 if ($this->accounttype === self::USER_TYPE && !$this->status) {
282 $this->login = mb_strtolower($this->login);
286 if ($this->
id == 1) {
298 $group =
new group($this->dbaccess);
301 $group->idgroup = $gid;
304 $err = $this->synchroAccountDocument();
310 return $this->synchroAccountDocument();
318 if ($this->
id == 1) {
323 $this->login = mb_strtolower($this->login);
330 include_once (
"WHAT/Class.Session.php");
331 include_once (
"FDL/Lib.Usercard.php");
333 $group =
new Group($this->dbaccess, $this->
id);
334 $ugroups = $group->groups;
336 $sql = sprintf(
"delete from groups where iduser=%d or idgroup=%d", $this->
id, $this->
id);
342 $action->session->CloseUsers($this->
id);
357 $query->basic_elem->sup_where = array(
358 "login='" . pg_escape_string(
$login) .
"'"
375 static $tdn = array();
379 if (isset($tdn[$uid]))
return $tdn[$uid];
381 $res = pg_query(
$dbid,
"select firstname, lastname from users where id=$uid");
382 if (pg_num_rows(
$res) > 0) {
383 $arr = pg_fetch_array(
$res, 0);
384 if ($arr[
"firstname"]) $tdn[$uid] = $arr[
"firstname"] .
' ' . $arr[
"lastname"];
385 else $tdn[$uid] = $arr[
"lastname"];
397 return trim(sprintf(
"%s %s", $this->firstname, $this->lastname));
410 simpleQuery(
'', sprintf(
"select id from users where fid=%d",
$fid) , $uid,
true,
true);
425 simpleQuery(
'', sprintf(
"select fid from users where id=%d", $uid) ,
$fid,
true,
true);
435 function setUsers(
$fid, $lname, $fname,
$expires,
$passdelay,
$login,
$status, $pwd1, $pwd2, $unused =
'', $extmail =
'')
455 function updateUser(
$fid, $lname, $fname,
$expires,
$passdelay,
$login,
$status, $pwd1, $pwd2, $extmail =
'', array $roles = array(-1
458 $this->lastname = $lname;
459 $this->firstname = $fname;
463 if ($pwd1 == $pwd2 and $pwd1 <>
"") {
467 if ($extmail !=
"") {
468 $this->mail = trim($extmail);
470 $this->mail = $this->
getMail();
476 $this->passdelay = 0;
483 $err = $this->Modify();
485 if ($roles != array(-1
507 if ($gname !=
"") $this->lastname = $gname;
508 if (($this->login ==
"") && (
$login !=
"")) $this->login =
$login;
510 $this->mail = $this->
getMail();
513 $this->accounttype = self::GROUP_TYPE;
516 $err = $this->Modify();
518 if ($roles != array(-1
532 $this->select($this->
id);
539 if (intval($this->passdelay) == 0) {
541 $this->passdelay =
"0";
543 else if (intval($this->
expires) == 0) {
548 function synchroAccountDocument()
552 if (
$dbaccess ==
"")
return _(
"no freedom DB access");
553 if ($this->fid <>
"") {
554 include_once (
"FDL/freedom_util.php");
560 $err = $iuser->RefreshDocUser();
563 include_once (
"FDL/Lib.Dir.php");
565 elseif ($this->accounttype == self::GROUP_TYPE) $fam = "IGROUP";
566 elseif ($this->accounttype == self::
ROLE_TYPE) $fam = "ROLE";
567 else $fam = "IUSER";;
569 "us_whatid = '" . $this->
id . "'"
579 $iuser->SetValue(
"US_WHATID", $this->
id);
581 $this->fid = $iuser->id;
582 $this->
modify(
true, array(
585 $err = $iuser->refreshDocUser();
591 $this->fid = $iuser->id;
592 $this->
modify(
true, array(
595 $err = $iuser->RefreshDocUser();
604 $salt_space =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
605 srand((
double)microtime() * 1000000);
606 for ($i = 0; $i < 16; $i++) $salt.= $salt_space[rand(0, strlen($salt_space) - 1) ];
607 $passk = crypt($pass,
"\$5\${$salt}");
608 $this->_deleteUserSessions();
616 if ($this->accounttype !=
'U')
return false;
617 return ($this->
checkpass($pass, $this->password));
622 if (substr($passk, 0, 3) !=
'$5$') {
624 $salt = substr($passk, 0, 2);
625 $passres = crypt($pass, $salt);
626 if ($passres == $passk) {
633 $log =
new Log(
"",
"Session",
"Authentication");
634 $facility = constant(
getParam(
"AUTHENT_LOGFACILITY",
"LOG_AUTH"));
635 $log->wlog(
"S", sprintf(
'User %s password crypted with salted SHA256 algorithm.', $this->login) , NULL, $facility);
639 $salt = substr($passk, 3, 19);
640 $passres = crypt($pass,
"\$5\${$salt}");
642 return ($passres == $passk);
651 if ($this->accounttype ==
'U') {
652 if (!$this->mail)
return '';
656 $dn = trim($this->firstname .
' ' . $this->lastname);
657 $mail = sprintf(
'"%s" <%s>', str_replace(
'"',
'-', $dn) , $this->mail);
661 $sql = sprintf(
"with recursive amembers(uid) as (
662 select iduser, users.login, users.mail from groups,users where idgroup = %d and users.id=groups.iduser
664 select iduser, users.login, users.mail from groups,users, amembers where groups.idgroup = amembers.uid and users.id=groups.iduser
665 ) select users.firstname, users.lastname, users.mail from amembers, users where users.id=amembers.uid and users.accounttype='U' and users.mail is not null order by users.mail;", $this->
id);
669 foreach ($umail as $aMail) {
671 $tMail[] = $aMail[
"mail"];
673 $tMail = array_unique($tMail);
675 foreach ($umail as $aMail) {
676 $dn = trim($aMail[
"firstname"] .
' ' . $aMail[
"lastname"]);
677 $tMail[] = sprintf(
'"%s" <%s>', str_replace(
'"',
'-', $dn) , $aMail[
"mail"]);
680 return implode(
', ', $tMail);
686 $group =
new group($this->dbaccess);
688 $userAdmin=
new Account($this->dbaccess);
691 $userAdmin->lastname =
"Master";
692 $userAdmin->firstname =
"Dynacase Platform";
693 $userAdmin->password_new =
"anakeen";
694 $userAdmin->login =
"admin";
695 $userAdmin->Add(
true);
697 $group->iduser = $userAdmin->id;
701 $groupAll=
new Account($this->dbaccess);
703 $groupAll->lastname =
"Utilisateurs";
704 $groupAll->firstname =
"";
705 $groupAll->login =
"all";
706 $groupAll->accounttype = self::GROUP_TYPE;
707 $groupAll->Add(
true);
709 $group->idgroup = $groupAll->id;
714 $anonymousUser=
new Account($this->dbaccess);
716 $anonymousUser->lastname =
"anonymous";
717 $anonymousUser->firstname =
"guest";
718 $anonymousUser->login =
"anonymous";
719 $anonymousUser->password =
"-";
720 $anonymousUser->accounttype = self::USER_TYPE;
721 $anonymousUser->Add(
true);
725 $groupAdmin=
new Account($this->dbaccess);
727 $groupAdmin->lastname =
"Administrateurs";
728 $groupAdmin->firstname =
"";
729 $groupAdmin->login =
"gadmin";
730 $groupAdmin->accounttype = self::GROUP_TYPE;
731 $groupAdmin->Add(
true);
746 if ($this->
id == 1)
return '';
748 if ($doc->
control($acl,
true) !=
'') {
749 foreach ($incumbents as $aIncumbent) {
750 $eErr = $doc->
controlUserId($doc->profid, $aIncumbent, $acl);
769 $query->order_by =
"lastname";
770 $query->AddQuery(
"(accountType='U')");
771 if ($filteruser)
$query->AddQuery(
"(login ~* '" . pg_escape_string($filteruser) .
"')" .
" or " .
"(lastname ~* '" . pg_escape_string($filteruser) .
"')");
782 $query->order_by =
"lastname";
783 $query->AddQuery(
"(accountType='G')");
784 $l =
$query->Query(0, 0, $qtype);
785 return (
$query->nb > 0) ? $l : array();
795 $query->order_by =
"lastname";
796 $query->AddQuery(
"(accountType='R')");
797 $l =
$query->Query(0, 0, $qtype);
798 return (
$query->nb > 0) ? $l : array();
808 $query->AddQuery(
"(accountType='G' or accountType='U')");
810 $query->order_by =
"accounttype, lastname";
811 return (
$query->Query(0, 0, $qtype));
819 $sql = sprintf(
"select idgroup from groups, users where groups.idgroup=users.id and users.accounttype='G' and groups.iduser=%d", $this->
id);
831 $list =
$query->Query(0, 0,
"TABLE",
"select users.* from users, groups where " .
"groups.iduser=users.id and " .
"idgroup=$id ;");
836 foreach (
$list as $k => $v) {
838 if ($v[
"accounttype"] ==
"G") {
839 if (!in_array($v[
"id"],
$r)) {
840 array_push(
$r, $v[
"id"]);
841 $uid+= $this->GetRUsersList($v[
"id"],
$r);
858 if (
$onlygroup) $optgroup =
" and users.accounttype='G' ";
860 $list =
$query->Query(0, 0,
"TABLE",
"select users.* from users, groups where " .
"groups.iduser=users.id and " .
"idgroup=$gid $optgroup;");
864 foreach (
$list as $k => $v) {
875 private function getUserMembers()
879 $g =
new Group($this->dbaccess);
880 $lg = $g->getChildsGroupId($this->
id);
882 $cond = getSqlCond($lg,
"idgroup",
true);
883 if (!$cond) $cond =
"true";
887 $sql = sprintf(
"SELECT distinct on (%s, users.id) users.id, users.login, users.firstname , users.lastname, users.mail,users.fid from users, groups where %s and (groups.iduser=users.id) %s and accounttype='U' order by %s", $sort, $cond, $condname, $sort);
901 if ($accountFilter) {
902 $acond = sprintf(
"and users.accounttype='%s'", pg_escape_string($accountFilter));
904 $sql = sprintf(
"with recursive agroups(gid) as (
905 select idgroup from groups,users where iduser = %d and users.id=groups.idgroup
907 select idgroup from groups,users, agroups where groups.iduser = agroups.gid and users.id=groups.idgroup
908 ) select users.* from agroups, users where users.id=agroups.gid %s order by lastname", $this->
id, $acond);
921 if (!$uid)
return array();
923 $sql = sprintf(
"with recursive agroups(gid, login, actype) as (
924 select idgroup, users.login, users.accounttype from groups,users where iduser = %d and users.id=groups.idgroup
926 select idgroup, users.login, users.accounttype from groups,users, agroups where groups.iduser = agroups.gid and users.id=groups.idgroup
927 ) select gid from agroups;", $uid);
940 if (!$this->
id)
return array();
944 $sql = sprintf(
"select id from users where substitute=%d;", $this->
id);
946 foreach ($incumbents as $aIncumbent) {
952 $lg = array_values(array_unique($lg));
953 $this->memberof =
'{' . implode(
',', $lg) .
'}';
958 if ($updateSubstitute && $this->substitute) {
959 $u =
new Account($this->dbaccess, $this->substitute);
960 $u->updateMemberOf(
false);
973 if (strlen($this->memberof) > 2) {
974 $memberOf = explode(
',', substr($this->memberof, 1, -1));
978 simpleQuery($this->dbaccess, sprintf(
"select fid from users where id in (%s)", implode(
',',
$memberOf)) , $dUids,
true);
995 if ($action->user->id == $uid) {
997 else $memberOf = $action->user->getMemberOf();
1000 if ($u->isAffected()) {
1017 $g =
new Group($this->dbaccess);
1018 $lg = $g->getChildsGroupId($this->
id);
1020 $cond = getSqlCond($lg,
"idgroup",
true);
1021 if (!$cond) $cond =
"true";
1023 $sql = sprintf(
"select users.id from users, groups where %s and (groups.iduser=users.id) and users.id=%d and isgroup != 'Y'", $cond, $uid);
1027 return ($result !=
'');
1041 $query->order_by =
"accounttype desc, lastname";
1042 $selgroup =
"and (accounttype='U')";
1043 if ($withgroup) $selgroup =
"";
1044 return (
$query->Query(0, $limit, $qtype,
"select users.* from users, groups where " .
"groups.iduser=users.id and " .
"idgroup={$this->id} {$selgroup};"));
1054 if ($limit !=
'all') $limit = intval($limit);
1056 $sql = sprintf(
"select * from users where memberof && '{%d}' and accounttype='U' order by lastname limit %s", $this->
id, $limit);
1058 $sql = sprintf(
"select * from users where memberof && '{%d}' order by accounttype, lastname limit %s", $this->
id, $limit);
1078 if ($expireDelay === - 1 || $expireDelay ===
false) {
1081 if ($context && (count($context) > 0)) {
1082 $scontext = serialize($context);
1083 }
else $scontext =
'';
1086 throw new Dcp\Exception(sprintf(
"User token : account must be affected"));
1088 include_once (
'WHAT/Class.UserToken.php');
1089 include_once (
'WHAT/Class.QueryDb.php');
1093 if (!$oneshot && !$forceCreate) {
1094 $q =
new QueryDb($this->dbaccess,
"UserToken");
1095 $q->addQuery(sprintf(
"userid=%d", $this->
id));
1096 $q->addQuery(sprintf(
"expire='%s'", $expireDate));
1097 if ($scontext)
$q->addQuery(
"context='" . pg_escape_string($scontext) .
"'");
1098 $tu =
$q->Query(0, 0,
"TABLE");
1099 $create = (
$q->nb == 0);
1109 $uk->token = $uk->genToken();
1111 $uk->expire = $uk->setExpiration($expireDelay);
1112 if ($uk->expire ===
false) {
1113 throw new Dcp\Exception(sprintf(
"User token : Invalid date. Expire must be a delay in seconds"));
1115 $uk->expendable = $oneshot;
1116 $uk->context = $scontext;
1121 $token = $uk->token;
1123 $token = $tu[0][
"token"];
1148 include_once (
'WHAT/Lib.Prefix.php');
1152 if ($this->
id != 1) {
1153 $err = sprintf(
"Method %s can only be used on the admin user.", __FUNCTION__);
1157 $supervisorDir = $pubdir . DIRECTORY_SEPARATOR .
'supervisor';
1158 $tmpFile = @tempnam($supervisorDir,
'.htpasswd');
1159 if ($tmpFile ===
false) {
1160 $err = sprintf(
"Error creating temporary file in '%s'.", $supervisorDir);
1163 if (chmod($tmpFile, 0600) ===
false) {
1164 $err = sprintf(
"Error setting mode 0600 on temporary file '%s'.", $tmpFile);
1168 $passwdLine = sprintf(
"%s:{SHA}%s",
'admin', base64_encode(sha1(
$admin_passwd,
true)));
1169 if (file_put_contents($tmpFile, $passwdLine) ===
false) {
1170 $err = sprintf(
"Error writing to temporary file '%s'.", $tmpFile);
1174 $htpasswdFile = $supervisorDir . DIRECTORY_SEPARATOR .
'.htpasswd';
1175 if (rename($tmpFile, $htpasswdFile) ===
false) {
1176 $err = sprintf(
"Error renaming temporary file '%s' to '%s'.", $tmpFile, $htpasswdFile);
1190 if ($this->accounttype != self::USER_TYPE)
return ErrorCode::getError(
"ACCT0003", $idRole, $this->login);
1191 if (!is_numeric($idRole)) {
1192 simpleQuery($this->dbaccess, sprintf(
"select id from users where login = '%'", pg_escape_string($idRole)) , $idRoleW,
true,
true);
1193 if ($idRoleW) $idRole = $idRoleW;
1195 if (!is_numeric($idRole)) {
1198 $g =
new group($this->dbaccess);
1199 $g->idgroup = $idRole;
1217 if ($this->accounttype == self::ROLE_TYPE)
return ErrorCode::getError(
"ACCT0007", implode(
',', $roleIds) , $this->login);
1220 if ($this->accounttype == self::USER_TYPE || $this->accounttype == self::GROUP_TYPE) {
1221 $g =
new group($this->dbaccess);
1222 foreach ($roleIds as $rid) {
1223 if (!is_numeric($rid)) {
1224 simpleQuery($this->dbaccess, sprintf(
"select id from users where login = '%'", pg_escape_string($rid)) , $idRoleW,
true,
true);
1225 if ($idRoleW) $rid = $idRoleW;
1227 if (!is_numeric($rid)) {
1233 $gerr = $g->add(
true);
1234 if ($gerr ==
'OK') $gerr =
'';
1241 if ($this->accounttype == self::GROUP_TYPE) {
1243 $lu = $this->getUserMembers();
1244 $uw =
new Account($this->dbaccess);
1245 foreach ($lu as $u) {
1247 $uw->updateMemberOf();
1259 $returnColumn = $useSystemId ?
"id" :
"fid";
1260 $sql = sprintf(
"SELECT users.%s from users, groups where groups.iduser=%d and users.id = groups.idgroup and users.accounttype='R'", $returnColumn, $this->
id);
1272 if (empty($mo))
return array();
1273 $sql = sprintf(
"SELECT * from users where id in (%s) and accounttype='R'", implode(
',', $mo));
1284 if ($this->accounttype == self::ROLE_TYPE)
return ErrorCode::getError(
"ACCT0005", $this->login);
1286 $sql = sprintf(
"DELETE FROM groups USING users where groups.iduser=%d and users.id=groups.idgroup and users.accounttype='R'", $this->
id);
1290 $err =
simpleQuery($this->dbaccess,
"delete from permission where computed");
1295 private function _deleteUserSessions()
1300 $session =
new Session($this->dbaccess);
1301 $session->deleteUserSessionsExcept($this->
id);
getAllMembers($limit="all", $onlyUsers=true)
static getUserAndGroupList($qtype="LIST")
static getFidFromUid($uid)
getGroupUserList($qtype="LIST", $withgroup=false, $limit="all")
Add($nopost=false, $nopre=false)
Exception class use exceptionCode to identifiy correctly exception.
static getExpirationDate($delayInSeconds)
static getUserMemberOf($uid, $strict=false)
static getUserList($qtype="LIST", $start=0, $slice=0, $filteruser= '')
getRUsersList($id, $r=array())
updateMemberOf($updateSubstitute=true)
updateUser($fid, $lname, $fname, $expires, $passdelay, $login, $status, $pwd1, $pwd2, $extmail= '', array $roles=array(-1), $substitute=-1)
refreshGroups($groupIdList, $refresh=false, &$currentPath=array(), &$groupDepth=array())
static getError($code, $args=null)
modify($nopost=false, $sfields="", $nopre=false)
getRoles($useSystemId=true)
getUsersGroupList($gid, $onlygroup=false)
setSubstitute($substitute)
setUsers($fid, $lname, $fname, $expires, $passdelay, $login, $status, $pwd1, $pwd2, $unused= '', $extmail= '')
setAdminHtpasswd($admin_passwd)
getParam($name, $def="")
must be in core or global type
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
static getGroupList($qtype="LIST")
static getDisplayName($uid)
getUserParents($accountFilter= '')
internalGetDocCollection($dbaccess, $dirid, $start="0", $slice="ALL", $sqlfilters=array(), $userid=1, $qtype="LIST", $fromid="", $distinct=false, $orderby="title", $latest=true, $trash="", &$debug=null, $folderRecursiveLevel=2, $join= '',\SearchDoc &$searchDoc=null)
getIncumbentPrivilege(Doc &$doc, $acl)
if(!$core->user->isAffected()) if($_POST["passwd1"]!=$_POST["passwd2"]) if($_POST["passwd1"]=="") $core user password_new
CheckLogin($login, $unused, $whatid)
static getRoleList($qtype="LIST")
deprecatedFunction($msg= '')
getUserToken($expireDelay=-1, $oneshot=false, $context=array(), $description="", $forceCreate=false)
static getUidFromFid($fid)
new_Doc($dbaccess, $id= '', $latest=false)
getStrictMemberOf($uid=-1)
setGroups($fid, $gname, $login, array $roles=array(-1))
if(($docid!==0)&&(!is_numeric($docid))) $query
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
computepass($pass, &$passk)
if($file) if($subject==""&&$file) if($subject=="") $err
getIncumbents($returnSystemIds=true)
getMemberOf($useSystemId=true)
setSupervisorHtpasswd($admin_passwd)
setLogin($login, $unused= '0')
controlUserId($docid, $uid, $aclname)
control($aclname, $strict=false)