00001 <?php
00013 include_once("core/system/JBaseObject.class.php");
00014 include_once("core/system/User.class.php");
00015 include_once("core/system/Group.class.php");
00016 include_once("core/system/Role.class.php");
00017 include_once("core/utils/JSession.class.php");
00018
00019
00020 define(JUGRSYSTEM_SESSION_VAR, "james-jugrsystem");
00021
00022
00033 class JUgrSystem extends JBaseObject
00034 {
00035 var $users;
00036 var $userslogin;
00037 var $groups;
00038 var $roles;
00039
00040
00049 function JUgrSystem()
00050 {
00051
00052 $this->RefreshAll();
00053 }
00054
00058 function RefreshAll()
00059 {
00060 $this->_RefreshUsers();
00061 $this->_RefreshGroups();
00062 $this->_RefreshRoles();
00063 }
00064
00071 function &GetInstance()
00072 {
00073 static $obj;
00074
00075 if (!isset($obj))
00076 {
00077
00078 if (JSession::ExistsVar(JUGRSYSTEM_SESSION_VAR))
00079 {
00080 $obj = JSession::GetVar(JUGRSYSTEM_SESSION_VAR);
00081 }
00082 else
00083 {
00084 $obj = new JUgrSystem();
00085 JSession::SetVar(JUGRSYSTEM_SESSION_VAR, $obj);
00086 }
00087
00088
00089
00090
00091
00092 register_shutdown_function(array(&$obj, _UpdateSession));
00093
00094 }
00095
00096 return $obj;
00097 }
00098
00099
00103 function _UpdateSession()
00104 {
00105 JSession::SetVar(JUGRSYSTEM_SESSION_VAR, $this);
00106 }
00107
00111 function _RefreshUsers()
00112 {
00113 $this->users = array();
00114 $this->userslogin = array();
00115
00116 $this->_UpdateSession();
00117 }
00118
00131 function &GetUser($id, $refresh=false, $cached=false)
00132 {
00133
00134
00135 if (isset($this->users[$id]))
00136 {
00137 if (!$refresh)
00138 return $this->users[$id];
00139 else
00140 {
00141 $user =& new User($id);
00142 if (JSystem::IsError($user))
00143 return JSystem::RaiseError(JSystem::GetLastError());
00144 else
00145 {
00146 $this->users[$user->GetId()] = $user;
00147 $this->userslogin[$user->GetLogin()] = $user->GetId();
00148 return $this->users[$user->GetId()];
00149 }
00150 }
00151 }
00152 else if (isset($this->userslogin[$id]))
00153 {
00154 if (!$refresh)
00155 return $this->users[$this->userslogin[$id]];
00156 else
00157 {
00158 $user =& new User($id);
00159 if (JSystem::IsError($user))
00160 return JSystem::RaiseError(JSystem::GetLastError());
00161 else
00162 {
00163 $this->users[$user->GetId()] = $user;
00164 $this->userslogin[$user->GetLogin()] = $user->GetId();
00165 return $this->users[$user->GetId()];
00166 }
00167 }
00168 }
00169 else
00170 {
00171 $user =& new User($id);
00172 if (JSystem::IsError($user))
00173 return JSystem::RaiseError(JSystem::GetLastError());
00174
00175 if ($cached)
00176 {
00177 $this->users[$user->GetId()] = $user;
00178 $this->userslogin[$user->GetLogin()] = $user->GetId();
00179 return $this->users[$user->GetId()];
00180 }
00181 else
00182 {
00183 return $user;
00184 }
00185 }
00186 }
00187
00192 function &GetAllUsers()
00193 {
00194 return User::GetAllUsersId();
00195 }
00196
00205 function &AddUser($login, $pass, $globalgroup, $globalrole)
00206 {
00207 $user =& User::Create($login, $pass, $globalgroup, $globalrole);
00208 $this->RefreshAll();
00209
00210 return $user;
00211 }
00212
00217 function RemoveUser($idU)
00218 {
00219 User::Remove($idU);
00220 $this->RefreshAll();
00221 }
00222
00226 function _RefreshGroups()
00227 {
00228 $groupsid = Group::GetAllGroupsId();
00229
00230 while (! $groupsid->EOF)
00231 {
00232 $idG = $groupsid->row["idG"];
00233 $this->groups[$idG] =& new Group($idG);
00234 $groupsid->Next();
00235 }
00236
00237 $this->_UpdateSession();
00238 }
00239
00248 function &GetGroup($idG, $refresh=false)
00249 {
00250 $group =& $this->_AccessGroup($idG, $refresh);
00251 if (JSystem::IsError($group))
00252 {
00253
00254 $this->_RefreshGroups();
00255 $group =& $this->_AccessGroup($idG, $refresh);
00256
00257 if (JSystem::IsError($group))
00258 {
00259 return JSystem::RaiseError(
00260 new JSystemError(ERROR_GROUP_NOT_FOUND));
00261 }
00262 else
00263 return $group;
00264 }
00265 else
00266 return $group;
00267 }
00268
00276 function &_AccessGroup($idG, $refresh=false)
00277 {
00278 if (isset($this->groups[$idG]))
00279 {
00280 if ($refresh)
00281 {
00282 $this->groups[$idG] =& new Group($idG);
00283 $this->_UpdateSession();
00284 }
00285
00286 return $this->groups[$idG];
00287 }
00288 else
00289 return JSystem::RaiseError(
00290 new JSystemError(ERROR_GROUP_NOT_FOUND));
00291 }
00292
00298 function &GetAllGroups($refresh = false)
00299 {
00300 if ($refresh)
00301 $this->_RefreshGroups();
00302 return $this->groups;
00303 }
00304
00313 function &GetGroups($parent=0, $global=null)
00314 {
00315 $result = array();
00316
00317 $groupsid = Group::GetAllGroupsId($parent, $global);
00318 while (! $groupsid->EOF)
00319 {
00320 $idG = $groupsid->row["idG"];
00321 $result[$idG] =& $this->GetGroup($idG);
00322 $groupsid->Next();
00323 }
00324
00325 return $result;
00326 }
00327
00328
00336 function &AddGroup($name, $global, $parent)
00337 {
00338 $group =& Group::Create($name, $global, $parent);
00339 if (JSystem::IsError($group))
00340 return JSystem::RaiseError(JSystem::GetLastError());
00341
00342 $this->groups[$group->GetId()] =& $group;
00343 $this->RefreshAll();
00344
00345 return $group;
00346 }
00347
00353 function RemoveGroup($idG)
00354 {
00355 $error = Group::Remove($idG);
00356
00357 if (JSystem::IsError($error))
00358 return $error;
00359 else
00360 {
00361 unset($this->groups[$idG]);
00362 $this->RefreshAll();
00363 }
00364 }
00365
00369 function _RefreshRoles()
00370 {
00371 $rolesid = Role::GetAllRolesId();
00372
00373 while (! $rolesid->EOF)
00374 {
00375 $idR = $rolesid->row["idR"];
00376 $this->roles[$idR] =& new Role($idR);
00377 $rolesid->Next();
00378 }
00379
00380 $this->_UpdateSession();
00381 }
00382
00391 function &GetRole($idR, $refresh=false)
00392 {
00393 $role =& $this->_AccessRole($idR, $refresh);
00394 if (JSystem::IsError($role))
00395 {
00396
00397 $this->_RefreshRoles();
00398 $role =& $this->_AccessRole($idR, $refresh);
00399
00400 if (JSystem::IsError($role))
00401 {
00402 return JSystem::RaiseError(
00403 new JSystemError(ERROR_ROLE_NOT_FOUND));
00404 }
00405 else
00406 return $role;
00407 }
00408 else
00409 return $role;
00410 }
00411
00419 function &_AccessRole($idR, $refresh=false)
00420 {
00421 if (isset($this->roles[$idR]))
00422 {
00423 if ($refresh)
00424 {
00425 $this->roles[$idR] =& new Role($idR);
00426 $this->_UpdateSession();
00427 }
00428
00429 return $this->roles[$idR];
00430 }
00431 else
00432 return JSystem::RaiseError(
00433 new JSystemError(ERROR_ROLE_NOT_FOUND));
00434 }
00435
00441 function &GetAllRoles($refresh=false)
00442 {
00443 if ($refresh)
00444 $this->_RefreshRoles();
00445 return $this->roles;
00446 }
00447
00453 function &AddRole($name)
00454 {
00455 $role =& Role::Create($name);
00456 $this->roles[$role->GetId()] =& $role;
00457 $this->RefreshAll();
00458
00459 return $role;
00460 }
00461
00467 function RemoveRole($idR)
00468 {
00469 $error = Role::Remove($idR);
00470 if (JSystem::IsError($error))
00471 return $error;
00472 else
00473 {
00474 unset($this->roles[$idR]);
00475 $this->RefreshAll();
00476 }
00477 }
00478
00479 }
00480 ?>