Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

User.class.php

Go to the documentation of this file.
00001 <?php
00006 include_once("core/system/JBaseObject.class.php");
00007 include_once("core/db/dbjames.class.php");
00008 include_once("core/system/JUgrSystem.class.php");
00009 include_once("core/system/JSystemError.class.php");
00010 include_once("core/modules/Modules.class.php");
00011 
00012 
00016 class User extends JBaseObject
00017 {
00018         var $data;      
00019         var $roles;     
00020         
00026         function User ($id)
00027         {
00028                 $dbObj = new DBJames();         
00029 
00030                 // We differentiate between getting the idU of the user and
00031                 // getting its login name.
00032                 $query  = " SELECT * FROM Users";
00033                 $query .= " WHERE idU = ".$id;
00034                 $dbObj->Query($query);
00035                 
00036                 if ($dbObj->numRows < 1)
00037                 {
00038                         $query  = " SELECT * FROM Users";
00039                         $query .= " WHERE login = '".$id."'";
00040                         $dbObj->Query($query);
00041                 }
00042 
00043                 
00044                 if ($dbObj->numRows == 1)
00045                 {
00046                         $this->data = $dbObj->row;
00047                         $this->_RefreshGroupsRoles();
00048                         // Get the global group role
00049                         //$this->roles[GLOBAL_GROUP] = $this->_GetGRId();
00050                 }
00051                 else
00052                 {
00053                         $this->data = null;
00054                         $this->RaiseConstructorError(new JSystemError(ERROR_USER_NOT_FOUND));
00055                 }
00056                 
00057                 /* This feature is removed while a better way of doing it is thinked 
00058                 $config =  new ConfigJames();
00059                 
00060                 $userModule = $config->getSystemConf("UserModule");
00061                 
00062                 if($dbObj->numRows == 1){
00063                         if($userModule){
00064                                 //echo "-($userModule,$idU)-#";
00065                                 $this->data = ThrowEvent("getData",$userModule,$idU);
00066                                 //echo "#--";
00067                                 // Por compatibilidad hacia atras (y por comodidad):
00068                                 $this->data["idU"] = $this->data["idUsuario"];
00069                                 //$this->data["name"] = $this->data["login"];
00070                                 $this->data["name"] = $this->data["nombre"]." ".$this->data["apellidos"];
00071                                 $this->data["globalGroup"] .= $dbObj->row["globalGroup"];                       
00072                                 $this->data["globalRole"] .= $dbObj->row["globalRole"]; 
00073                                  
00074                         }else{
00075                                 $this->data = $dbObj->row;
00076                         }
00077                 }else{
00078                         $this->data = 0;
00079                 } */
00080         }
00081         
00085         function _RefreshGroupsRoles()
00086         {
00087                 $this->roles = array();
00088                 $groups =& $this->_GetGroupsRoles();
00089                 while($groups && (!$groups->EOF))
00090                 {
00091                         $this->roles[$groups->row["idG"]] = $groups->row["idR"];
00092                         $groups->Next();
00093                 }
00094         }
00095 
00100         function &_GetGroupsRoles()
00101         {
00102                 $dbObj = new DBJames();
00103 
00104                 $query  = " SELECT Groups.*, RelUsersGroups.idR FROM ";
00105                 $query .= " RelUsersGroups,Groups ";
00106                 $query .= " WHERE RelUsersGroups.idU = ".$this->data["idU"];
00107                 $query .= " AND   Groups.idG = RelUsersGroups.idG";
00108                 $query .= " ORDER BY Groups.name DESC";
00109 
00110                 $dbObj->Query($query);
00111                 
00112                 return $dbObj;
00113         }
00114         
00119         function GetId()
00120         {
00121                 return $this->data["idU"];
00122         }
00123         
00128         function GetLogin()
00129         {
00130                 return $this->data["login"];
00131         }
00132         
00139         function IsPassword($password)
00140         {
00141                 return ($this->data["pass"] == $password);
00142         }
00143         
00148         function &GetGlobalGroup()
00149         {
00150                 $ugrsystem =& JUgrSystem::GetInstance();
00151                 
00152                 return $ugrsystem->GetGroup($this->_GetGGId());
00153         }
00154         
00159         function &GetGlobalRole()
00160         {
00161                 $ugrsystem =& JUgrSystem::GetInstance();
00162                 
00163                 return $ugrsystem->GetRole($this->_GetGRId());
00164         }
00165         
00170         function _GetGGId()
00171         {
00172                 return $this->data["globalGroup"];
00173         }
00174         
00179         function _GetGRId()
00180         {
00181                 return $this->data["globalRole"];
00182         }
00183         
00188         function SetLogin($name)
00189         {
00190                 $this->data["login"] = $name;
00191         }
00192         
00198         function SetPassword($password)
00199         {
00200                 $this->data["pass"] = $password;
00201         }
00202         
00208         function SetGlobalGroup($gg)
00209         {
00210                 if (! $gg->IsGlobal())
00211                         return JSystem::RaiseError(
00212                                                 new JSystemError(ERROR_NOT_GLOBAL_GROUP));
00213                 
00214                 $this->data["globalGroup"] = $gg->GetId();
00215         }
00216         
00221         function SetGlobalRole($gr)
00222         {
00223                 $this->data["globalRole"] = $gr->GetId();
00224                 
00225                 // Update the info
00226                 //$this->roles[GLOBAL_GROUP] = $gr->GetId();
00227         }
00228         
00232         function Commit()
00233         {
00234                 $dbObj = new DBJames();
00235                 
00236                 $query  = " UPDATE Users SET ";
00237                 $query .= " login = ".$this->GetLogin().", ";
00238                 $query .= " pass = ".$this->data["pass"].", ";
00239                 $query .= " globalGroup = ". $this->data["globalGroup"].", ";
00240                 $query .= " globalRole = ". $this->data["globalRole"];
00241                 $query .= " WHERE idU = ".$this->GetId();
00242                 
00243                 $dbObj->Execute($query);
00244         }
00245         
00250         function GetGroups()
00251         {
00252                 $ugrsystem =& JUgrSystem::GetInstance();
00253                 
00254                 if($this->data == null)
00255                         return JSystem::RaiseError(
00256                                         new JSystemError(ERROR_NOT_INITIALIZED));
00257 
00258                 $result = array();
00259                 
00260                 if (count($this->roles) > 0)
00261                 {
00262                         foreach($this->roles as $idG => $idR)
00263                         {
00264                                 $result[$idG] =& $ugrsystem->GetGroup($idG);
00265                         }
00266                 }               
00267                         
00268                 return $result;
00269         }
00270 
00271 
00277         function &GetModules()
00278         {
00279                 if($this->data == null)
00280                         return JSystem::RaiseError(
00281                                         new JSystemError(ERROR_NOT_INITIALIZED));
00282 
00283                 $modules =& Modules::GetInstance();
00284                 $dbObj = new DBJames();
00285 
00286                 $query  = " SELECT DISTINCT Modules.* FROM ";
00287                 $query .= " Modules,RelModulesGroups,RelUsersGroups ";
00288                 $query .= " WHERE RelUsersGroups.idU = ".$this->data["idU"];
00289                 $query .= " AND RelModulesGroups.idG = RelUsersGroups.idG ";
00290                 $query .= " AND Modules.idM = RelModulesGroups.idM ";
00291 
00292                 $dbObj->Query($query);
00293             
00294                 $result = array();
00295                 
00296                 while (!$dbObj->EOF)
00297                 {       
00298                         $idM = $dbObj->row["idM"];
00299                         $result[$idM] =& $modules->GetModule($idM);
00300                         $dbObj->Next();
00301                 }               
00302                         
00303                 return $result;
00304         }
00305           
00310         function &GetRoles()
00311         {
00312                 $ugrsystem =& JUgrSystem::GetInstance();
00313                 
00314                 if($this->data == null)
00315                         return JSystem::RaiseError(
00316                                         new JSystemError(ERROR_NOT_INITIALIZED));
00317 
00318                 $result = array();
00319                 
00320                 if (count($this->roles) > 0)
00321                 {
00322                         foreach($this->roles as $idG => $idR)
00323                         {
00324                                 $result[$idG] =& $ugrsystem->GetRole($idR);
00325                         }
00326                 }               
00327                         
00328                 return $result;
00329         }
00330                 
00336         function &GetRole($idG)
00337         {
00338                 $ugrsystem =& JUgrSystem::GetInstance();
00339                 
00340                 if ($idG == GLOBAL_GROUP)
00341                         return $this->GetGlobalRole();
00342                 elseif ($this->roles[$idG] > 0)
00343                         return $ugrsystem->GetRole($this->roles[$idG]);
00344                 else
00345                         return JSystem::RaiseError(
00346                                         new JSystemError(ERROR_NOT_IN_GROUP));
00347         }
00348         
00353         function &GetActiveRole()
00354         {
00355                 $context =& JContext::GetInstance();
00356                 return $this->GetRole($context->GetActiveGroupId());
00357         }
00358         
00365         function AddToGroup($idG, $idR)
00366         {
00367                 $ugrsystem =& JUgrSystem::GetInstance();
00368                 
00369                 if ($this->roles[$idG] > 0)
00370                 {
00371                         return JSystem::RaiseError(
00372                                                 new JSystemError(ERROR_ALREADY_IN_GROUP));
00373                 }
00374                 else if (JSystem::IsError($ugrsystem->GetGroup($idG)))
00375                 {
00376                         return JSystem::RaiseError(
00377                                                 new JSystemError(ERROR_GROUP_NOT_FOUND));
00378                 }               
00379                 else if (JSystem::IsError($ugrsystem->GetRole($idR)))
00380                 {
00381                         return JSystem::RaiseError(
00382                                                 new JSystemError(ERROR_ROLE_NOT_FOUND));
00383                 }
00384                 else
00385                 {
00386                         $group =& $ugrsystem->GetGroup($idG);
00387                         $result = $group->AddUser($this->GetId(), $idR);
00388                         if (JSystem::IsError($result))
00389                                 return $result;
00390                         else
00391                                 $this->_RefreshGroupsRoles();
00392                 }               
00393         }
00394         
00400         function SetRole($idG, $idR) 
00401         {
00402                 $ugrsystem =& JUgrSystem::GetInstance();
00403                 
00404                 if (! isset($this->roles[$idG]))
00405                 {
00406                         return JSystem::RaiseError(
00407                                                 new JSystemError(ERROR_NOT_IN_GROUP));
00408                 }               
00409                 else if (JSystem::IsError($ugrsystem->GetRole($idR)))
00410                 {
00411                         return JSystem::RaiseError(
00412                                                         new JSystemError(ERROR_ROLE_NOT_FOUND));
00413                 }
00414                 else
00415                 {
00416                         $dbObj = new DBJames();
00417                         
00418                         $query  = " UPDATE RelUsersGroups SET ";
00419                         $query .= " idR = ".$idR;
00420                         $query .= " WHERE idU = ".$this->GetId();
00421                         $query .= " AND idG = ".$idG;
00422                         
00423                         $dbObj->Execute($query);
00424                         
00425                         $this->_RefreshGroupsRoles();
00426                 }               
00427         }
00428                                                  
00429 
00434         function RemoveFromGroup($idG)
00435         {
00436                 $ugrsystem =& JUgrSystem::GetInstance();
00437                 
00438                 if (! isset($this->roles[$idG]))
00439                 {
00440                         return JSystem::RaiseError(new JSystemError(ERROR_NOT_IN_GROUP));
00441                 }
00442                 else
00443                 {
00444                         $group =& $ugrsystem->GetGroup($idG);
00445                         $result = $group->RemoveUser($this->GetId());
00446                         if (JSystem::IsError($result))
00447                                 return $result;
00448                         else
00449                                 $this->_RefreshGroupsRoles();
00450                 }               
00451         }
00452 
00459         function HasPermission($name)
00460         {
00461                 $role = $this->GetActiveRole();
00462                 $module = JSystem::GetCurrentModule();
00463                 return $role->HasPermission($name, $module->GetId());
00464         }
00465         
00474         function &Create($login, $pass, $globalgroup, $globalrole)
00475         {
00476                 $ugrsystem =& JUgrSystem::GetInstance();
00477                 $group =& $ugrsystem->GetGroup($globalgroup);
00478                 
00479                 if (JSystem::IsError($group))
00480                 {
00481                         return JSystem::RaiseError(
00482                                                 new JSystemError(ERROR_GROUP_NOT_FOUND));
00483                 }
00484                 else if (! $group->IsGlobal())
00485                 {
00486                         return JSystem::RaiseError(
00487                                                 new JSystemError(ERROR_NOT_GLOBAL_GROUP));
00488                 }
00489                 else if (JSystem::IsError($ugrsystem->GetRole($globalrole)))
00490                 {
00491                         return JSystem::RaiseError(
00492                                                 new JSystemError(ERROR_ROLE_NOT_FOUND));
00493                 } 
00494                 else
00495                 {
00496                         $dbObj = new DBJames();
00497                         
00498                         $query  = " INSERT INTO Users ";
00499                         $query .= " (login, pass, globalGroup, globalRole) VALUES ";
00500                         $query .= " ('".$login."', '".$pass."', ".$globalgroup.", ".$globalrole.") ";
00501                         
00502                         $dbObj->Execute($query);
00503                         
00504                         $dbObj->SetInsertId();
00505                         
00506                         $user =& new User($dbObj->newID);
00507                         if (JSystem::IsError($user))
00508                                 return JSystem::GetLastError();
00509                         else
00510                                 return $user;
00511                 }
00512         }
00513         
00518         function Remove($idU)
00519         {
00520                 $ugrsystem =& JUgrSystem::GetInstance();
00521                 
00522                 if (JSystem::IsError($ugrsystem->GetUser($idU)))
00523                 {
00524                         return JSystem::RaiseError(
00525                                                 new JSystemError(ERROR_USER_NOT_FOUND));
00526                 }
00527                 else
00528                 {
00529                         $dbObj = new DBJames();
00530                         
00531                         // First, we remove the user from all groups.
00532                         $query  = " DELETE FROM RelUsersGroups ";
00533                         $query .= " WHERE idU = ".$idU;                 
00534                         $dbObj->Execute($query);
00535                         
00536                         // Then, we remove it from the Users table
00537                         $query  = " DELETE FROM Users ";
00538                         $query .= " WHERE idU = ".$idU;
00539                         $dbObj->Execute($query);
00540                 }
00541         }
00542         
00547         function GetAllUsersId()
00548         {
00549                 $dbObj = new DBJames();
00550                 
00551                 $query = " SELECT * FROM Users";
00552                 
00553                 $dbObj->Query($query);
00554                 
00555                 return $dbObj;
00556         }               
00557 }
00558 ?>

Generated on Wed Nov 19 20:29:35 2003 for James by doxygen 1.3.4