PrivilegeFactory.php

Go to the documentation of this file.
00001 <?PHP
00002 
00003 #
00004 #   FILE:  PrivilegeFactory.php
00005 #
00006 #   METHODS PROVIDED:
00007 #       PrivilegeFactory()
00008 #           - constructor
00009 #       SomeMethod($SomeParameter, $AnotherParameter)
00010 #           - short description of method
00011 #
00012 #   AUTHOR:  Edward Almasy
00013 #
00014 #   Part of the Collection Workflow Integration System
00015 #   Copyright 2007 Edward Almasy and Internet Scout
00016 #   http://scout.wisc.edu
00017 #
00018 
00024 class PrivilegeFactory extends ItemFactory {
00025 
00026     # ---- PUBLIC INTERFACE --------------------------------------------------
00027 
00030 
00032     public function PrivilegeFactory()
00033     {
00034         $this->ItemFactory("Privilege", "CustomPrivileges", "Id", "Name");
00035 
00036         $AllConstants = get_defined_constants(TRUE);
00037         $UserConstants = $AllConstants["user"];
00038 
00039         foreach ($UserConstants as $Name => $Value)
00040         {
00041             if (strpos($Name, "PRIV_") === 0)
00042             {
00043                 $this->PrivilegeConstants[$Value] = $Name;
00044             }
00045         }
00046     }
00047 
00052 
00060     public function GetPrivileges($IncludePredefined = TRUE, $ReturnObjects = TRUE)
00061     {
00062         # read custom privileges in from DB
00063         $PrivNames = $this->GetItemNames();
00064 
00065         # add in predefined privileges if requested
00066         if ($IncludePredefined)
00067         {
00068             global $G_PrivDescriptions;
00069             $PrivNames = $G_PrivDescriptions + $PrivNames;
00070         }
00071 
00072         # if caller requested objects to be returned
00073         if ($ReturnObjects)
00074         {
00075             # convert strings to objects and return to caller
00076             foreach ($PrivNames as $Id => $Name)
00077             {
00078                 $PrivObjects[$Id] = new Privilege($Id);
00079             }
00080             return $PrivObjects;
00081         }
00082         else
00083         {
00084             # return strings to caller
00085             return $PrivNames;
00086         }
00087     }
00088 
00094     public function GetPrivilegeWithName($Name)
00095     {
00096         global $G_PrivDescriptions;
00097 
00098         # predefined privilege constant name
00099         if (in_array($Name, $this->PrivilegeConstants))
00100         {
00101             $Id = array_search($Name, $this->PrivilegeConstants);
00102             $Privilege = new Privilege($Id);
00103 
00104             return $Privilege;
00105         }
00106 
00107         # predefined privilege constant description
00108         if (in_array($Name, $G_PrivDescriptions))
00109         {
00110             $ConstantName = array_search($Name, $G_PrivDescriptions);
00111             $Id = array_search($ConstantName, $this->PrivilegeConstants);
00112             $Privilege = new Privilege($Id);
00113 
00114             return $Privilege;
00115         }
00116 
00117         $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
00118 
00119         # custom privilege name
00120         foreach ($CustomPrivileges as $Id => $PrivilegeName)
00121         {
00122             if ($Name == $PrivilegeName)
00123             {
00124                 $Privilege = new Privilege($Id);
00125 
00126                 return $Privilege;
00127             }
00128         }
00129 
00130         return NULL;
00131     }
00132 
00138     public function GetPrivilegeWithValue($Value)
00139     {
00140         global $G_PrivDescriptions;
00141 
00142         # predefined privilege constant name
00143         if (array_key_exists($Value, $this->PrivilegeConstants))
00144         {
00145             $Privilege = new Privilege($Value);
00146 
00147             return $Privilege;
00148         }
00149 
00150         $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
00151 
00152         # custom privilege name
00153         foreach ($CustomPrivileges as $Id => $PrivilegeName)
00154         {
00155             if ($Value == $Id)
00156             {
00157                 $Privilege = new Privilege($Id);
00158 
00159                 return $Privilege;
00160             }
00161         }
00162 
00163         return NULL;
00164     }
00165 
00170     public function GetPredefinedPrivilegeConstants()
00171     {
00172         return $this->PrivilegeConstants;
00173     }
00174 
00179 
00185     public function PrivilegeNameExists($Name)
00186     {
00187         global $G_PrivDescriptions;
00188 
00189         # predefined privilege constant name
00190         if (in_array($Name, $this->PrivilegeConstants))
00191         {
00192             return TRUE;
00193         }
00194 
00195         # predefined privilege constant description
00196         if (in_array($Name, $G_PrivDescriptions))
00197         {
00198             return TRUE;
00199         }
00200 
00201         $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
00202 
00203         # custom privilege name
00204         if (in_array($Name, $CustomPrivileges))
00205         {
00206             return TRUE;
00207         }
00208 
00209         return FALSE;
00210     }
00211 
00217     public function PrivilegeValueExists($Value)
00218     {
00219         # predefined privilege constant name
00220         if (array_key_exists($Value, $this->PrivilegeConstants))
00221         {
00222             return TRUE;
00223         }
00224 
00225         $CustomPrivileges = $this->GetPrivileges(FALSE);
00226 
00227         foreach ($CustomPrivileges as $Privilege)
00228         {
00229             if ($Value == $Privilege->Id())
00230             {
00231                 return TRUE;
00232             }
00233         }
00234 
00235         return FALSE;
00236     }
00237 
00240     # ---- PRIVATE INTERFACE -------------------------------------------------
00241 
00242     private $PrivilegeConstants = array();
00243 
00244 }