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

JSystem.class.php

Go to the documentation of this file.
00001 <?php
00006 include_once("core/system/JBaseObject.class.php");
00007 include_once("core/system/JContext.class.php");
00008 include_once("core/system/JUgrSystem.class.php");
00009 include_once("core/system/JErrorHandler.class.php");
00010 include_once("core/config/JConfig.class.php");
00011 
00012 define("CURRENT_USER", "jsystem-current-user");
00013 define("CURRENT_GROUP", "jsystem-current-group");
00014 define("CURRENT_MODULE", "jsystem-current-module");
00015 define(JUGRSYSTEM, "james-jugrsystem");
00016 
00017 
00028 class JSystem extends JBaseObject
00029 {
00033         function JSystem()
00034         {
00035                 JSession::CleanAll();
00036                 
00037                 $context =& JContext::GetInstance();            
00038                 $context->SetLogged(false);
00039         }
00048         function Login($login, $pass)
00049         {
00050                 include_once("core/auth/JAuth.class.php");
00051                 
00052                 $auth =& JAuth::GetAuthSystem();
00053 
00054                 // If the authentication is ok
00055                 if ($auth->Authenticate($login, $pass))
00056                 {
00057                         $ugrsystem =& JUgrSystem::GetInstance();
00058                         $context =& JContext::GetInstance();
00059                         
00060                         // We get the user and cache it in session.
00061                         $user = $ugrsystem->GetUser($login, true, true);
00062                         
00063                         // Now we set the initial context.
00064                         $context->SetLogged(true);
00065                         $context->SetActiveUserId($user->GetId());
00066                         $context->SetAnonymous(false);
00067                         
00068                         // We set the current group and role as the global 
00069                         // ones by default
00070                         $gr =& $user->GetGlobalRole();
00071                         $context->SetActiveGroupId(GLOBAL_GROUP);
00072                         $context->SetActiveRoleId($gr->GetId());
00073                         
00074                         // And return true, because all was ok
00075                         return true;
00076                 }
00077                 else
00078                         return false;           
00079         }
00080         
00085         function AnonLogin()
00086         {
00087                 // If the system is not configured to allow anonymous access,
00088                 // we'll return false and do nothing.
00089                 if (!JSystem::GetConfig("AnonAccess"))
00090                         return false;
00091                 else
00092                 {
00093                         $ugrsystem =& JUgrSystem::GetInstance();
00094                         $context =& JContext::GetInstance();
00095                         
00096                         // We get the anonymous user's id.
00097                         $anonid = JSystem::GetConfig("IdAnonUser"); 
00098                         
00099                         // We get the user and cache it in session.
00100                         $user = $ugrsystem->GetUser($anonid, true, true);
00101                         
00102                         // Now we set the initial context.
00103                         $context->SetLogged(true);
00104                         $context->SetActiveUserId($anonid);
00105                         $context->SetAnonymous(true);
00106                         
00107                         // We set the current group and role as the global 
00108                         // ones by default
00109                         $gr =& $user->GetGlobalRole();
00110                         $context->SetActiveGroupId(GLOBAL_GROUP);
00111                         $context->SetActiveRoleId($gr->GetId());
00112                                 
00113                         // And return true, because everything was ok
00114                         return true;
00115                 }
00116         }
00117         
00122         function IsLogged()
00123         {               
00124                 $context =& JContext::GetInstance();
00125                 return $context->IsLogged();
00126         }               
00127         
00132         function IsAnonymous()
00133         {               
00134                 $context =& JContext::GetInstance();
00135                 return $context->IsAnonymous();
00136         }               
00137         
00141         function Logout()
00142         {
00143                 // We just set the context as non-logged.
00144                 $context =& JContext::GetInstance();
00145                 
00146                 // Unload all modules
00147                 while ($context->GetActiveModuleId() != NULL)
00148                         JSystem::UnloadModule();
00149                         
00150                 $context->SetLogged(false);
00151                 
00152                 // Finally, we clear the session
00153                 JSession::CleanAll();
00154         }
00155         
00163         function LoadModuleGroup($group, $module, $idRMG)
00164         {
00165                 $result = false;
00166                 
00167                 // Get the context...
00168                 $context =& JContext::GetInstance();
00169                 $ugrsystem =& JUgrSystem::GetInstance();
00170                 
00171                 // The current user, group and role...
00172                 $user =& $ugrsystem->GetUser($context->GetActiveUserId());
00173                 $grobj =& $ugrsystem->GetGroup($group);
00174                 // The group does not exist
00175                 if (JSystem::IsError($grobj))
00176                         return false;
00177                 $role =& $user->GetRole($group);
00178                 // The role does not exist
00179                 if (JSystem::IsError($role))
00180                         return false;
00181                 
00182                 // Then, we check if the user can load this module
00183                 $modules =& $grobj->GetModules();               
00184                 if ($modules[$idRMG]->GetId() == $module)
00185                         if (! $role->HasPermission("hidden", $module))
00186                         {
00187                                 $result = true;
00188                                 
00189                                 // Set the context properly
00190                                 $context->SetActiveGroupId($group);
00191                                 $context->SetActiveRoleId($role->GetId());
00192                                 
00193                                 // And load the module
00194                                 JSystem::LoadModule($module, $idRMG);                           
00195                         }
00196                         
00197                 return $result;
00198         }
00199         
00206         function LoadModuleGlobalGroup($module, $idRMG)
00207         {
00208                 $result = false;
00209                 
00210                 // Get the context...
00211                 $context =& JContext::GetInstance();
00212                 $ugrsystem =& JUgrSystem::GetInstance();
00213                 
00214                 // The current user, group and role...
00215                 $user =& $ugrsystem->GetUser($context->GetActiveUserId());
00216                 $grobj =& $user->GetGlobalGroup();
00217                 // If the group does not exist
00218                 if (JSystem::IsError($grobj))
00219                         return false;
00220                 $role =& $user->GetGlobalRole();
00221                 // If the role does not exist 
00222                 if (JSystem::IsError($role))
00223                         return false;
00224                 
00225                 // Then, we check if the user can load this module
00226                 $modules =& $grobj->GetModules();               
00227                 if ($modules[$idRMG]->GetId() == $module)
00228                         if (! $role->HasPermission("hidden", $module))
00229                         {
00230                                 $result = true;
00231                                 
00232                                 // Set the context properly
00233                                 $context->SetActiveGroupId(GLOBAL_GROUP);
00234                                 $context->SetActiveRoleId($role->GetId());
00235                                 
00236                                 // And load the module
00237                                 JSystem::LoadModule($module, $idRMG);                           
00238                         }
00239                         
00240                 return $result;
00241         }
00242         
00248         function LoadModule($module, $idRMG=0)
00249         {
00250                 include_once("core/comms/ThrowEvent.inc.php");
00251                 
00252                 $context =& JContext::GetInstance();
00253                 
00254                 $context->PushActiveModule($module, $idRMG);
00255                 
00256                 // Throws the load event to the module that is being loaded.
00257                 ThrowEvent("load", $module, $context->GetActiveGroupId());
00258         }                               
00259                 
00260         
00264         function UnloadModule()
00265         {
00266                 include_once("core/comms/ThrowEvent.inc.php");
00267                 
00268                 $context =& JContext::GetInstance();
00269                 
00270                 if ($context->GetActiveModuleId() != NULL)
00271                 {
00272                         $idM = $context->PopActiveModule();
00273                         ThrowEvent("unload", $idM, $context->GetActiveGroupId());
00274                 }
00275         }
00276         
00283         function &InitModule($name)
00284         {
00285                 if (!JSystem::IsLogged())
00286                         return JSystem::RaiseError(new JSystemError(ERROR_SESSION_EXPIRED));
00287                         
00288                 $current =& JSystem::GetCurrentModule();
00289                 $modules =& Modules::GetInstance();
00290                 $init =& $modules->GetModule($name);
00291                 
00292                 if (JSystem::IsError($init))
00293                         return JSystem::RaiseError(JSystem::GetLastError());
00294 
00295                 if ($init->GetId() != $current->GetId())
00296                         JSystem::LoadModule($init->GetId());
00297                         
00298                 // Set the global variables:
00299                 $context =& JContext::GetInstance();
00300                 $ugrsystem =& JUgrSystem::GetInstance();
00301                 
00302                 $GLOBALS[CURRENT_USER] =& $ugrsystem->GetUser($context->GetActiveUserId());
00303                 if (!$context->IsGlobalGroup())
00304                         $GLOBALS[CURRENT_GROUP] =& $ugrsystem->GetGroup($context->GetActiveGroupId());
00305                 else
00306                         $GLOBALS[CURRENT_GROUP] = GLOBAL_GROUP;
00307                 $GLOBALS[CURRENT_MODULE] =& $init;
00308                 $GLOBALS[JUGRSYSTEM] =& $ugrsystem;             
00309                         
00310                 return $init;
00311         }
00312         
00317         function &GetCurrentUser()
00318         {
00319                 $context =& JContext::GetInstance();
00320                 $ugrsystem =& JUgrSystem::GetInstance();
00321                 
00322                 return $ugrsystem->GetUser($context->GetActiveUserId());
00323         }
00324         
00329         function &GetCurrentGroup()
00330         {
00331                 $context =& JContext::GetInstance();
00332                 $ugrsystem =& JUgrSystem::GetInstance();
00333                 
00334                 if (!$context->IsGlobalGroup())
00335                 {
00336                         return $ugrsystem->GetGroup($context->GetActiveGroupId());
00337                 }
00338                 else
00339                         return GLOBAL_GROUP;
00340         }
00341         
00346         function IsGlobalGroup()
00347         {
00348                 $context =& JContext::GetInstance();            
00349                 return $context->IsGlobalGroup();
00350         }
00351         
00356         function &GetCurrentModule()
00357         {
00358                 include_once("core/modules/Modules.class.php");
00359                 
00360                 $context =& JContext::GetInstance();
00361                 $modules =& Modules::GetInstance();
00362                 
00363                 return $modules->GetModule($context->GetActiveModuleId());
00364         }
00365         
00366         
00372         function GetConfig($key)
00373         {
00374                 include(CONFIG_PATH . CORE_CONFIG_FILE);
00375                 if (!isset($config[$key]))
00376                 {
00377                         $jconfig = JConfig::GetInstance(CONFIG_SYSTEM_SCOPE);
00378                         $val = $jconfig->GetValue($key);
00379                 }
00380                 else
00381                         $val = $config[$key];
00382         
00383                 return $val;
00384         }
00385         
00393         function SetConfig($keys, $user=true, $group=false)
00394         {
00395                 $config =& JConfig::GetInstance(CONFIG_SYSTEM_SCOPE);
00396                 $config->SetValue($keys, $user, $group);                
00397         }
00398 
00399         
00405         function IsError($error)
00406         {
00407                 return JErrorHandler::IsError($error);
00408         }
00409         
00414         function GetLastError()
00415         {
00416                 $ehandler =& JErrorHandler::GetInstance();
00417                 return $ehandler->PopError();
00418         }               
00419 }
00420 ?>

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