CWIS Developer Documentation
PrivilegeFactory.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: PrivilegeFactory.php
5 #
6 # Part of the Collection Workflow Integration System (CWIS)
7 # Copyright 2007-2013 Edward Almasy and Internet Scout Research Group
8 # http://scout.wisc.edu/cwis/
9 #
10 
17 
18  # ---- PUBLIC INTERFACE --------------------------------------------------
19 
22 
24  public function __construct()
25  {
26  parent::__construct("Privilege", "CustomPrivileges", "Id", "Name");
27 
28  $AllConstants = get_defined_constants(TRUE);
29  $UserConstants = $AllConstants["user"];
30 
31  foreach ($UserConstants as $Name => $Value)
32  {
33  if (strpos($Name, "PRIV_") === 0)
34  {
35  $this->PrivilegeConstants[$Value] = $Name;
36  }
37  }
38  }
39 
44 
52  public function GetPrivileges($IncludePredefined = TRUE, $ReturnObjects = TRUE)
53  {
54  # if caller wants predefined privileges included
55  if ($IncludePredefined)
56  {
57  # get complete list of privilege names
58  $PrivNames = $this->GetItemNames();
59  }
60  else
61  {
62  # read in only custom privileges from DB
63  $PrivNames = parent::GetItemNames();
64  }
65 
66  # if caller requested objects to be returned
67  if ($ReturnObjects)
68  {
69  $PrivObjects = array();
70 
71  # convert strings to objects and return to caller
72  foreach ($PrivNames as $Id => $Name)
73  {
74  $PrivObjects[$Id] = new Privilege($Id);
75  }
76 
77  return $PrivObjects;
78  }
79  else
80  {
81  # return strings to caller
82  return $PrivNames;
83  }
84  }
85 
91  public function GetPrivilegeWithName($Name)
92  {
93  global $G_PrivDescriptions;
94 
95  # predefined privilege constant name
96  if (in_array($Name, $this->PrivilegeConstants))
97  {
98  $Id = array_search($Name, $this->PrivilegeConstants);
99  $Privilege = new Privilege($Id);
100 
101  return $Privilege;
102  }
103 
104  # predefined privilege constant description
105  if (in_array($Name, $G_PrivDescriptions))
106  {
107  $ConstantName = array_search($Name, $G_PrivDescriptions);
108  $Id = array_search($ConstantName, $this->PrivilegeConstants);
109  $Privilege = new Privilege($Id);
110 
111  return $Privilege;
112  }
113 
114  $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
115 
116  # custom privilege name
117  foreach ($CustomPrivileges as $Id => $PrivilegeName)
118  {
119  if ($Name == $PrivilegeName)
120  {
121  $Privilege = new Privilege($Id);
122 
123  return $Privilege;
124  }
125  }
126 
127  return NULL;
128  }
129 
135  public function GetPrivilegeWithValue($Value)
136  {
137  global $G_PrivDescriptions;
138 
139  # predefined privilege constant name
140  if (array_key_exists($Value, $this->PrivilegeConstants))
141  {
142  $Privilege = new Privilege($Value);
143 
144  return $Privilege;
145  }
146 
147  $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
148 
149  # custom privilege name
150  foreach ($CustomPrivileges as $Id => $PrivilegeName)
151  {
152  if ($Value == $Id)
153  {
154  $Privilege = new Privilege($Id);
155 
156  return $Privilege;
157  }
158  }
159 
160  return NULL;
161  }
162 
168  {
169  return $this->PrivilegeConstants;
170  }
171 
179  function GetItemNames($SqlCondition = NULL)
180  {
181  $Names = parent::GetItemNames($SqlCondition);
182  $Names = $Names + $GLOBALS["G_PrivDescriptions"];
183  asort($Names);
184  return $Names;
185  }
186 
191 
197  public function PrivilegeNameExists($Name)
198  {
199  global $G_PrivDescriptions;
200 
201  # predefined privilege constant name
202  if (in_array($Name, $this->PrivilegeConstants))
203  {
204  return TRUE;
205  }
206 
207  # predefined privilege constant description
208  if (in_array($Name, $G_PrivDescriptions))
209  {
210  return TRUE;
211  }
212 
213  $CustomPrivileges = $this->GetPrivileges(FALSE, FALSE);
214 
215  # custom privilege name
216  if (in_array($Name, $CustomPrivileges))
217  {
218  return TRUE;
219  }
220 
221  return FALSE;
222  }
223 
229  public function PrivilegeValueExists($Value)
230  {
231  # predefined privilege constant name
232  if (array_key_exists($Value, $this->PrivilegeConstants))
233  {
234  return TRUE;
235  }
236 
237  $CustomPrivileges = $this->GetPrivileges(FALSE);
238 
239  foreach ($CustomPrivileges as $Privilege)
240  {
241  if ($Value == $Privilege->Id())
242  {
243  return TRUE;
244  }
245  }
246 
247  return FALSE;
248  }
249 
252  # ---- PRIVATE INTERFACE -------------------------------------------------
253 
254  private $PrivilegeConstants = array();
255 
256 }
User rights management framework allowing custom privege definition.
Definition: Privilege.php:16
GetPrivilegeWithName($Name)
Get the Privilege object with the given name.
__construct()
Object constructor.
GetPredefinedPrivilegeConstants()
Get all predefined privilege constants and their values.
Factory which extracts all defined privileges from the database.
PrivilegeNameExists($Name)
Determine if a privilege with the given name exists.
GetItemNames($SqlCondition=NULL)
Retrieve human-readable privilege names.
GetPrivileges($IncludePredefined=TRUE, $ReturnObjects=TRUE)
Get all privileges.
Common factory class for item manipulation.
Definition: ItemFactory.php:17
GetPrivilegeWithValue($Value)
Get the Privilege object with the given value.
PrivilegeValueExists($Value)
Determine if a privilege with the given value exists.