aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/grum_plugins_classes-2/users_groups.class.inc.php
blob: 235199f7c41af7fb8d33597683f6849c91c2224b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php

/* -----------------------------------------------------------------------------
  class name: allowed_access, groups, users
  class version: 1.0
  date: 2007-10-31
  ------------------------------------------------------------------------------
  author: grum at grum.dnsalias.com
  << May the Little SpaceFrog be with you >>
  ------------------------------------------------------------------------------

   this classes provides base functions to manage users/groups access
  groups and users classes extends allowed_access classes

    - constructor allowed_access($alloweds="")
    - constructor groups($alloweds="")
    - constructor users($alloweds="")
    - (public) function get_list()
    - (public) function set_allowed($id, $allowed)
    - (public) function set_alloweds()
    - (public) function get_alloweds($return_type)
    - (public) function is_allowed($id)
    - (public) function html_view($sep=", ", $empty="")
    - (public) function html_form($basename)
    - (private) function init_list()
   ---------------------------------------------------------------------- */
class allowed_access
{
  var $access_list;

  /*
    constructor initialize the groups_list
  */
  function allowed_access($alloweds = "")
  {
    $this->init_list();
    $this->set_alloweds($alloweds);
  }

  /*
    initialize the groups list
  */
  function init_list()
  {
    $this->access_list=array();
  }

  /*
    returns list (as an array)
  */
  function get_list()
  {
    return($this->access_list);
  }

  /*
    set element an allowed state
  */
  function set_allowed($id, $allowed)
  {
    if(isset($this->access_list[$id]))
    {
      $this->access_list[$id]['allowed']=$allowed;
    }
  }

  /*
    set a group enabled/disabled state
  */
  function set_state($id, $enabled)
  {
    if(isset($this->access_list[$id]))
    {
      $this->access_list[$id]['enabled']=$enabled;
    }
  }

  /*
    set alloweds list
    $list is string of id, separated with "/"
  */
  function set_alloweds($list)
  {
    $alloweds=explode("/", $list);
    $alloweds=array_flip($alloweds);
    foreach($this->access_list as $key => $val)
    {
      if(isset($alloweds[$key]))
      {
        $this->access_list[$key]['allowed']=true;
      }
      else
      {
        $this->access_list[$key]['allowed']=false;
      }
    }
  }

  /*
    get alloweds list
    return a string of groups, separated with "/"
  */
  function get_alloweds($return_type = 'name')
  {
    $returned="";
    foreach($this->access_list as $key => $val)
    {
      if($val['allowed'])
      { $returned.=$val[$return_type]."/"; }
    }
    return($returned);
  }


  /*
    returns true if is allowed
  */
  function is_allowed($id)
  {
    if(isset($this->access_list[$id]))
    { return($this->access_list[$id]['allowed']); }
    else
    { return(false); }
  }

  /*
    returns true if all or one is allowed
      ids is an array
  */
  function are_allowed($ids, $all=false)
  {
    foreach($ids as $val)
    {
      if($all)
      {
        if(!$this->is_allowed($val))
        {
          return(false);
        }
      }
      else
      {
        if($this->is_allowed($val))
        {
          return(true);
        }        
      }
    }
    return(false);
  }

  /*
    returns an HTML list with label rather than id
  */
  function html_view($sep=", ", $empty="")
  {
    $returned="";
    foreach($this->access_list as $key => $val)
    {
      if($val['allowed'])
      {
        if($returned!="")
        {
          $returned.=$sep;
        }
        $returned.=$val['name'];
      }
    }
    if($returned=="")
    {
      $returned=$empty;
    }
    return($returned);
  }
  /*
    returns a generic HTML form to manage the groups access
  */
  function html_form($basename)
  {
    /*
    <!-- BEGIN allowed_group_row -->
    <label><input type="checkbox" name="fmypolls_att_allowed_groups_{allowed_group_row.ID}" {allowed_group_row.CHECKED}/>&nbsp;{allowed_group_row.NAME}</label>
    <!-- END allowed_group_row -->
    */
    $text='';
    foreach($this->access_list as $key => $val)
    {
      if($val['allowed'])
      {
        $checked=' checked';
      }
      else
      {
        $checked='';
      }

      if($val['enabled'])
      {
        $enabled='';
      }
      else
      {
        $enabled=' disabled';
      }

      $text.='<label><input type="checkbox" name="'.$basename.$val['id'].'" '.$checked.$enabled.'/>
          &nbsp;'.$val['name'].'</label>&nbsp;';
    }
    return($text);
  }
} //allowed_access








/* ----------------------------------------------------------------------
   this class provides base functions to manage groups access
    init_list redefined to initialize access_list from database GROUPS
   ---------------------------------------------------------------------- */
class groups extends allowed_access
{
  /*
    initialize the groups list
  */
  function init_list()
  {
    $this->access_list=array();
    $sql="SELECT id, name FROM ".GROUPS_TABLE." ORDER BY name";
    $result=pwg_query($sql);
    if($result)
    {
      while($row=mysql_fetch_assoc($result))
      {
        $this->access_list[$row['id']] =
                   array('id' => $row['id'],
                         'name' => $row['name'],
                         'allowed' => false,
                         'enabled' => true);
      }
    }
  }
}








/* -----------------------------------------------------------------------------
   this class provides base functions to manage users access
----------------------------------------------------------------------------- */
class users extends allowed_access
{
  /*
    constructor
  */
  function users($alloweds = "")
  {
    parent::allowed_access($alloweds);
    $this->set_state('admin', false);
    $this->set_allowed('admin', true);
  }

  /*
    initialize the groups list
  */
  function init_list()
  {
    $users_list = array('guest', 'generic', 'normal', 'admin');
    $this->access_list=array();
    foreach($users_list as $val)
    {
      $this->access_list[$val] =
                  array('id' => $val,
                        'name' => l10n('user_status_'.$val),
                        'allowed' => false,
                        'enabled' => true);
    }
  }
} //class users



?>