CWIS Developer Documentation
PrivilegeFactory.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: PrivilegeFactory.php
5 #
6 # METHODS PROVIDED:
7 # PrivilegeFactory()
8 # - constructor
9 # SomeMethod($SomeParameter, $AnotherParameter)
10 # - short description of method
11 #
12 # AUTHOR: Edward Almasy
13 #
14 # Part of the Collection Workflow Integration System
15 # Copyright 2007 Edward Almasy and Internet Scout
16 # http://scout.wisc.edu
17 #
18 
25 
26  # ---- PUBLIC INTERFACE --------------------------------------------------
27 
30 
32  public function PrivilegeFactory()
33  {
34  $this->ItemFactory("Privilege", "CustomPrivileges", "Id", "Name");
35 
36  $AllConstants = get_defined_constants(TRUE);
37  $UserConstants = $AllConstants["user"];
38 
39  foreach ($UserConstants as $Name => $Value)
40  {
41  if (strpos($Name, "PRIV_") === 0)
42  {
43  $this->PrivilegeConstants[$Value] = $Name;
44  }
45  }
46  }
47 
52 
60  public function GetPrivileges($IncludePredefined = TRUE, $ReturnObjects = TRUE)
61  {
62  # if caller wants predefined privileges included
63  if ($IncludePredefined)
64  {
65  # get complete list of privilege names
66  $PrivNames = $this->GetItemNames();
67  }
68  else
69  {
70  # read in only custom privileges from DB
71  $PrivNames = parent::GetItemNames();
72  }
73 
74  # if caller requested objects to be returned
75  if ($ReturnObjects)
76  {
77  $PrivObjects = array();
78 
79  # convert strings to objects and return to caller
80  foreach ($PrivNames as $Id => $Name)
81  {
82  $PrivObjects[$Id] = new Privilege($Id);
83  }
84 
85  return $PrivObjects;
86  }
87  else
88  {
89  # return strings to caller
90  return $PrivNames;
91  }
92  }
93 
99  public function GetPrivilegeWithName($Name)
100  {
101  global $G_PrivDescriptions;
102 
103  # predefined privilege constant name
104  if (in_array($Name, $this->PrivilegeConstants))
105  {
106  $Id = array_search($Name, $this->PrivilegeConstants);
107  $Privilege = new Privilege($Id);
108 
109  return $Privilege;
110  }
111 
112  # predefined privilege constant description
113  if (in_array($Name, $G_PrivDescriptions))
114  {
115  $ConstantName = array_search($Name, $G_PrivDescriptions);
116  $Id = array_search($ConstantName, $this->PrivilegeConstants);
117  $Privilege = new Privilege($Id);
118 
119  return $Privilege;
120  }
121 
122  $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
123 
124  # custom privilege name
125  foreach ($CustomPrivileges as $Id => $PrivilegeName)
126  {
127  if ($Name == $PrivilegeName)
128  {
129  $Privilege = new Privilege($Id);
130 
131  return $Privilege;
132  }
133  }
134 
135  return NULL;
136  }
137 
143  public function GetPrivilegeWithValue($Value)
144  {
145  global $G_PrivDescriptions;
146 
147  # predefined privilege constant name
148  if (array_key_exists($Value, $this->PrivilegeConstants))
149  {
150  $Privilege = new Privilege($Value);
151 
152  return $Privilege;
153  }
154 
155  $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
156 
157  # custom privilege name
158  foreach ($CustomPrivileges as $Id => $PrivilegeName)
159  {
160  if ($Value == $Id)
161  {
162  $Privilege = new Privilege($Id);
163 
164  return $Privilege;
165  }
166  }
167 
168  return NULL;
169  }
170 
176  {
177  return $this->PrivilegeConstants;
178  }
179 
186  function GetItemNames($SqlCondition = NULL)
187  {
188  $Names = parent::GetItemNames($SqlCondition);
189  $Names = $Names + $GLOBALS["G_PrivDescriptions"];
190  asort($Names);
191  return $Names;
192  }
193 
198 
204  public function PrivilegeNameExists($Name)
205  {
206  global $G_PrivDescriptions;
207 
208  # predefined privilege constant name
209  if (in_array($Name, $this->PrivilegeConstants))
210  {
211  return TRUE;
212  }
213 
214  # predefined privilege constant description
215  if (in_array($Name, $G_PrivDescriptions))
216  {
217  return TRUE;
218  }
219 
220  $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
221 
222  # custom privilege name
223  if (in_array($Name, $CustomPrivileges))
224  {
225  return TRUE;
226  }
227 
228  return FALSE;
229  }
230 
236  public function PrivilegeValueExists($Value)
237  {
238  # predefined privilege constant name
239  if (array_key_exists($Value, $this->PrivilegeConstants))
240  {
241  return TRUE;
242  }
243 
244  $CustomPrivileges = $this->GetPrivileges(FALSE);
245 
246  foreach ($CustomPrivileges as $Privilege)
247  {
248  if ($Value == $Privilege->Id())
249  {
250  return TRUE;
251  }
252  }
253 
254  return FALSE;
255  }
256 
259  # ---- PRIVATE INTERFACE -------------------------------------------------
260 
261  private $PrivilegeConstants = array();
262 
263 }