source: extensions/GrumPluginClasses/classes/GPCUsersGroups.class.inc.php @ 5550

Last change on this file since 5550 was 5550, checked in by grum, 14 years ago

Release 3.0.0 : the plugin has been completely rewritten

  • Property svn:executable set to *
File size: 6.6 KB
Line 
1<?php
2
3/* -----------------------------------------------------------------------------
4  class name: GPCAllowedAccess, GPCGroups, GPCUsers
5  class version  : 2.0.0
6  plugin version : 3.0.0
7  date           : 2010-03-30
8  ------------------------------------------------------------------------------
9  author: grum at piwog.org
10  << May the Little SpaceFrog be with you >>
11  ------------------------------------------------------------------------------
12
13   this classes provides base functions to manage users/groups access
14  groups and users classes extends GPCAllowedAccess classes
15
16    - constructor GPCAllowedAccess($alloweds="")
17    - constructor groups($alloweds="")
18    - constructor users($alloweds="")
19    - (public) function getList()
20    - (public) function setAllowed($id, $allowed)
21    - (public) function setAlloweds()
22    - (public) function getAlloweds($return_type)
23    - (public) function isAllowed($id)
24    - (public) function htmlView($sep=", ", $empty="")
25    - (public) function htmlForm($basename)
26    - (private) function initList()
27
28
29| release | date       |
30| 1.1.0   | 2009/11/29 | * add 'webmaster' status for users
31|         |            |
32| 2.0.0   | 2010/03/28 | * Uses piwigo pwg_db_* functions instead of mysql_*
33|         |            |   functions
34|         |            | * update classes & functions names
35|         |            |
36|         |            |
37
38   ---------------------------------------------------------------------- */
39class GPCAllowedAccess
40{
41  public $access_list;
42
43  /*
44    constructor initialize the groups_getListgetListlist
45  */
46  public function __construct($alloweds = "")
47  {
48    $this->initList();
49    $this->setAlloweds($alloweds);
50  }
51
52  public function __destruct()
53  {
54    unset($this->access_list);
55  }
56
57  /*
58    initialize the groups list
59  */
60  protected function initList()
61  {
62    $this->access_list=array();
63  }
64
65  /*
66    returns list (as an array)
67  */
68  function getList()
69  {
70    return($this->access_list);
71  }
72
73  /*
74    set element an allowed state
75  */
76  function setAllowed($id, $allowed)
77  {
78    if(isset($this->access_list[$id]))
79    {
80      $this->access_list[$id]['allowed']=$allowed;
81    }
82  }
83
84  /*
85    set a group enabled/disabled state
86  */
87  function setState($id, $enabled)
88  {
89    if(isset($this->access_list[$id]))
90    {
91      $this->access_list[$id]['enabled']=$enabled;
92    }
93  }
94
95  /*
96    set alloweds list
97    $list is string of id, separated with "/"
98  */
99  function setAlloweds($list)
100  {
101    $alloweds=explode("/", $list);
102    $alloweds=array_flip($alloweds);
103    foreach($this->access_list as $key => $val)
104    {
105      if(isset($alloweds[$key]))
106      {
107        $this->access_list[$key]['allowed']=true;
108      }
109      else
110      {
111        $this->access_list[$key]['allowed']=false;
112      }
113    }
114  }
115
116  /*
117    get alloweds list
118    return a string of groups, separated with "/"
119  */
120  function getAlloweds($return_type = 'name')
121  {
122    $returned="";
123    foreach($this->access_list as $key => $val)
124    {
125      if($val['allowed'])
126      { $returned.=$val[$return_type]."/"; }
127    }
128    return($returned);
129  }
130
131
132  /*
133    returns true if is allowed
134  */
135  function isAllowed($id)
136  {
137    if(isset($this->access_list[$id]))
138    { return($this->access_list[$id]['allowed']); }
139    else
140    { return(false); }
141  }
142
143  /*
144    returns true if all or one is allowed
145      ids is an array
146  */
147  function areAllowed($ids, $all=false)
148  {
149    foreach($ids as $val)
150    {
151      if($all)
152      {
153        if(!$this->isAllowed($val))
154        {
155          return(false);
156        }
157      }
158      else
159      {
160        if($this->isAllowed($val))
161        {
162          return(true);
163        }
164      }
165    }
166    return(false);
167  }
168
169  /*
170    returns an HTML list with label rather than id
171  */
172  function htmlView($sep=", ", $empty="")
173  {
174    $returned="";
175    foreach($this->access_list as $key => $val)
176    {
177      if($val['allowed'])
178      {
179        if($returned!="")
180        {
181          $returned.=$sep;
182        }
183        $returned.=$val['name'];
184      }
185    }
186    if($returned=="")
187    {
188      $returned=$empty;
189    }
190    return($returned);
191  }
192  /*
193    returns a generic HTML form to manage the groups access
194  */
195  function htmlForm($basename)
196  {
197    /*
198    <!-- BEGIN allowed_group_row -->
199    <label><input type="checkbox" name="fmypolls_att_allowed_groups_{allowed_group_row.ID}" {allowed_group_row.CHECKED}/>&nbsp;{allowed_group_row.NAME}</label>
200    <!-- END allowed_group_row -->
201    */
202    $text='';
203    foreach($this->access_list as $key => $val)
204    {
205      if($val['allowed'])
206      {
207        $checked=' checked';
208      }
209      else
210      {
211        $checked='';
212      }
213
214      if($val['enabled'])
215      {
216        $enabled='';
217      }
218      else
219      {
220        $enabled=' disabled';
221      }
222
223      $text.='<label><input type="checkbox" name="'.$basename.$val['id'].'" '.$checked.$enabled.'/>
224          &nbsp;'.$val['name'].'</label>&nbsp;';
225    }
226    return($text);
227  }
228} //GPCAllowedAccess
229
230
231
232
233
234
235
236
237/* ----------------------------------------------------------------------
238   this class provides base functions to manage groups access
239    initList redefined to initialize access_list from database GROUPS
240   ---------------------------------------------------------------------- */
241class GPCGroups extends GPCAllowedAccess
242{
243  /*
244    initialize the groups list
245  */
246  protected  function initList()
247  {
248    $this->access_list=array();
249    $sql="SELECT id, name FROM ".GROUPS_TABLE." ORDER BY name";
250    $result=pwg_query($sql);
251    if($result)
252    {
253      while($row=pwg_db_fetch_assoc($result))
254      {
255        $this->access_list[$row['id']] =
256                   array('id' => $row['id'],
257                         'name' => $row['name'],
258                         'allowed' => false,
259                         'enabled' => true);
260      }
261    }
262  }
263}
264
265
266
267
268
269
270
271
272/* -----------------------------------------------------------------------------
273   this class provides base functions to manage users access
274----------------------------------------------------------------------------- */
275class GPCUsers extends GPCAllowedAccess
276{
277  /*
278    constructor
279  */
280  public function __construct($alloweds = "")
281  {
282    parent::__construct($alloweds);
283    $this->setState('admin', false);
284    $this->setAllowed('admin', true);
285  }
286
287  /*
288    initialize the groups list
289  */
290  protected function initList()
291  {
292    $users_list = array('guest', 'generic', 'normal', 'webmaster', 'admin');
293    $this->access_list=array();
294    foreach($users_list as $val)
295    {
296      $this->access_list[$val] =
297                  array('id' => $val,
298                        'name' => l10n('user_status_'.$val),
299                        'allowed' => false,
300                        'enabled' => true);
301    }
302  }
303} //class users
304
305
306
307?>
Note: See TracBrowser for help on using the repository browser.