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

Last change on this file since 9001 was 9001, checked in by grum, 13 years ago

release 3.4.0
keep compatibility with previous release

  • Property svn:executable set to *
File size: 5.8 KB
RevLine 
[5550]1<?php
2
3/* -----------------------------------------------------------------------------
4  class name: GPCAllowedAccess, GPCGroups, GPCUsers
[8961]5  class version  : 2.1.0
6  plugin version : 3.4.0
[5550]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
[8961]16    - constructor GPCAllowedAccess($alloweds = array(), $accessMode='a')
17    - constructor groups($alloweds = array(), $accessMode='a')
18    - constructor users($alloweds = array(), $accessMode='a')
[5550]19    - (public) function getList()
20    - (public) function setAllowed($id, $allowed)
[8961]21    - (public) function setAlloweds($idList, $allowed)
22    - (public) function getAlloweds()
[5550]23    - (public) function isAllowed($id)
24    - (private) function initList()
25
26
27| release | date       |
28| 1.1.0   | 2009/11/29 | * add 'webmaster' status for users
29|         |            |
30| 2.0.0   | 2010/03/28 | * Uses piwigo pwg_db_* functions instead of mysql_*
31|         |            |   functions
32|         |            | * update classes & functions names
33|         |            |
[8961]34| 2.1.0   | 2011/01/15 | * remove html function
[5550]35|         |            |
[8961]36|         |            | * implement accessMode
37|         |            |
38|         |            |
[5550]39
40   ---------------------------------------------------------------------- */
41class GPCAllowedAccess
42{
[9001]43  public $access_list;
[8961]44  protected $accessMode='a'; // 'a' : allowed, 'n' : not allowed
[5550]45
[8961]46  /**
47   * constructor initialize default values
48   *
49   * @param Array $alloweds  : list of items
50   *        String $alloweds : list of items (separator : '/')
51   *
52   * @param String $accessMode : 'a' = access is allowed by default for all values, $allowed param is a list of not allowed values
53   *                             'n' = access is not allowed by default for all values, $allowed param is a list of allowed values
54   *                             priority is given to the $allowed value
55   */
56  public function __construct($alloweds = array(), $accessMode='a')
[5550]57  {
58    $this->initList();
[8961]59    $this->setAlloweds($alloweds, $accessMode=='n');
[5550]60  }
61
62  public function __destruct()
63  {
[9001]64    unset($this->access_list);
[5550]65  }
66
[8961]67  /**
68   * destroy the groups list
69   */
[5550]70  protected function initList()
71  {
[9001]72    $this->access_list=array();
[5550]73  }
74
[8961]75  /**
76   * returns list of items (as an array)
77   * each array item is an array :
78   *  'id'      : (String) id of item
79   *  'name'    : (String) name of item
80   *  'allowed' : (Bool)   access is allowed or not
81   *
82   * @return Array
83   */
[5550]84  function getList()
85  {
[9001]86    return($this->access_list);
[5550]87  }
88
[8961]89  /**
90   * set allowed value for an item
91   *
92   * @param String $id : id of item
93   * @param Bool $allowed : access allowed or not
94   */
[5550]95  function setAllowed($id, $allowed)
96  {
[9001]97    if(isset($this->access_list[$id]))
[5550]98    {
[9001]99      $this->access_list[$id]['allowed']=$allowed;
[5550]100    }
101  }
102
[8961]103
104  /**
105   * set alloweds items (can be given as an array or a string with separator '/')
106   * according to the
107   *
108   * @param Array $idList  : list of items to set
109   * @param String $idList
110   * @param Bool $allowed : access allowed or not
111   */
112  function setAlloweds($idList, $allowed)
[5550]113  {
[8961]114    if(!is_array($idList)) $idList=explode("/", $idList);
[5550]115
[8961]116    $idList=array_flip($idList);
117
[9001]118    foreach($this->access_list as $key => $val)
[5550]119    {
[8961]120      if(isset($idList[$key]))
[5550]121      {
[9001]122        $this->access_list[$key]['allowed']=$allowed;
[5550]123      }
124      else
125      {
[9001]126        $this->access_list[$key]['allowed']=!$allowed;
[5550]127      }
128    }
129  }
130
[8961]131
132  /**
133   * return list of alloweds items
134   *
135   * @return Array
136   */
137  function getAlloweds()
[5550]138  {
[8961]139    $returned=Array();
140
[9001]141    foreach($this->access_list as $key => $val)
[5550]142    {
[8961]143      if($val['allowed']) $returned[]=$val;
[5550]144    }
145    return($returned);
146  }
147
148
[8961]149  /**
150   * returns true if is allowed
151   *
152   * @param String $id : item id
153   * @retrun Bool
154   */
[5550]155  function isAllowed($id)
156  {
[9001]157    if(isset($this->access_list[$id]))
[5550]158    {
[9001]159      return($this->access_list[$id]['allowed']);
[5550]160    }
[8961]161    else
[5550]162    {
[8961]163      return($this->accessMode=='a');
[5550]164    }
165  }
166
167} //GPCAllowedAccess
168
169
170
171
172
173
174
175
[8961]176/**
177 * ----------------------------------------------------------------------------
178 *  this class provides base functions to manage groups access
[9001]179 *  initList redefined to initialize access_list from database GROUPS
[8961]180 * ----------------------------------------------------------------------------
181 */
[5550]182class GPCGroups extends GPCAllowedAccess
183{
[8961]184  /**
185   * initialize the groups list
186   */
[5550]187  protected  function initList()
188  {
[9001]189    $this->access_list=array();
[5550]190    $sql="SELECT id, name FROM ".GROUPS_TABLE." ORDER BY name";
191    $result=pwg_query($sql);
192    if($result)
193    {
194      while($row=pwg_db_fetch_assoc($result))
195      {
[9001]196        $this->access_list[$row['id']]=array(
[8961]197            'id' => $row['id'],
198            'name' => $row['name'],
199            'allowed' => ($this->accessMode=='a')
200          );
[5550]201      }
202    }
203  }
204}
205
206
207
208
209
210
211
212
[8961]213/**
214 * ----------------------------------------------------------------------------
215 *  this class provides base functions to manage users access
[9001]216 *  initList redefined to initialize access_list from piwigo's predefined values
[8961]217 * ----------------------------------------------------------------------------
218 */
[5550]219class GPCUsers extends GPCAllowedAccess
220{
[8961]221  /**
222   * initialize the users list
223   */
[5550]224  protected function initList()
225  {
[8961]226    $usersList = array('guest', 'generic', 'normal', 'webmaster', 'admin');
[9001]227    $this->access_list=array();
[8961]228    foreach($usersList as $val)
[5550]229    {
[9001]230      $this->access_list[$val]=array(
[8961]231          'id' => $val,
232          'name' => l10n('user_status_'.$val),
233          'allowed' => ($this->accessMode=='a')
234        );
[5550]235    }
236  }
237} //class users
238
239
240
241?>
Note: See TracBrowser for help on using the repository browser.