Subversion Repositories php_users

Rev

Rev 20 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20 Rev 21
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
require_once( 'UsersDataSourceMySQLi.class.php' );
-
 
4
 
-
 
5
/*
3
/*
6
   Copyright (c) 2021, Daily Data, Inc. Redistribution and use in 
4
   Copyright (c) 2021, Daily Data, Inc. Redistribution and use in 
7
   source and binary forms, with or without modification, are permitted
5
   source and binary forms, with or without modification, are permitted
8
   provided that the following conditions are met:
6
   provided that the following conditions are met:
9
 
7
 
Line 42... Line 40...
42
 * class.
40
 * class.
43
 * 
41
 * 
44
 * To build a data access class for UsersPermissions, the following 5
42
 * To build a data access class for UsersPermissions, the following 5
45
 * methods must exist.
43
 * methods must exist.
46
 * getPassword(username)
44
 * getPassword(username)
47
 * getRecord(username)
-
 
48
 * getAllUsers()
45
 * getAllUsers()
49
 * getARecord
46
 * getARecord
50
 * update
47
 * update
51
 * 
48
 * 
52
 * Additionally, where appropriate, the following function is useful
49
 * Additionally, where appropriate, the following function is useful
Line 63... Line 60...
63
 * @version 0.9.0 (beta)
60
 * @version 0.9.0 (beta)
64
 * @copyright 2021 Daily Data, Inc.
61
 * @copyright 2021 Daily Data, Inc.
65
 * 
62
 * 
66
 */
63
 */
67
 
64
 
-
 
65
require_once( 'UsersDataSourceMySQLi.class.php' );
-
 
66
 
68
class usersPermissionsDataSource extends usersDataSource {
67
class usersPermissionsDataSourceMySQLi extends usersDataSourceMySQLi {
69
   
68
   
70
      /**
69
   /**
71
       * extends usersDataSource
70
    * extends usersDataSource
72
       * 
71
    * 
73
       * Adds the new fields needed for permissions. First, creates array
72
    * Adds the new fields needed for permissions. First, creates array
74
       * $permissions, then does a recursive merge into 
73
    * $permissions, then does a recursive merge into 
75
       * usersDataSource::$configuration. 
74
    * usersDataSource::$configuration. 
76
       * NOTE: we must merge this before we merge in $dbDef so the
75
    * NOTE: we must merge this before we merge in $dbDef so the
77
       * developer can modify permissions also
76
    * developer can modify permissions also
78
       * 
77
    * 
79
       * @param mysqli $dbConnection Existing mysqli database connection
78
    * @param mysqli $connection Existing mysqli database connection
80
       * @param string[] $dbDef Array to be merged with $configuration
79
    * @param string[] $customFields Array to be merged with $configuration
81
       * @param string[] $dbLoginInfo Array containing username, hostname, etc.. to make mysqli connection_aborted
80
    * @param string[] $dbLoginInfo Array containing username, hostname, etc.. to make mysqli connection_aborted
82
       * 
81
    * 
83
       * @return null
82
    * @return null
84
       * 
83
    * 
85
       */
84
    */
86
      public function __construct( $dbConnection = null, $dbDef = array(), $dbLoginInfo = array() ) {
85
   public function __construct( $connection, $customFields = array() ) {
87
         
-
 
88
      $permissions = array(
86
      $permissions = array(
89
         'tables' => array(
87
         'tables' => array(
90
            'permissions categories' => array(
88
            'permissions categories' => array(
91
               'table'     => '_permissions_categories',   // table name for user records
89
               'table'     => '_permissions_categories',   // table name for user records
92
               'id'        => '_permission_category_id', // ID column name
90
               'id'        => '_permission_category_id', // ID column name
Line 96... Line 94...
96
               'fields' => array(
94
               'fields' => array(
97
                  'name'  => array(
95
                  'name'  => array(
98
                        'dbColumn'     => 'name',
96
                        'dbColumn'     => 'name',
99
                        'type'         => 'varchar',
97
                        'type'         => 'varchar',
100
                        'size'         => 16,
98
                        'size'         => 16,
101
                        'required'     => true
99
                        'required'     => true,
-
 
100
                        'unique'       => true
102
                        )
101
                        )
103
                  ),
102
                  ),
104
               ), // permissions categories table
103
               ), // permissions categories table
105
            'permissions' => array(
104
            'permissions' => array(
106
               'table'     => '_permissions',   // table name for user records
105
               'table'     => '_permissions',   // table name for user records
Line 111... Line 110...
111
               'fields' => array(
110
               'fields' => array(
112
                  'name'  => array(
111
                  'name'  => array(
113
                        'dbColumn'     => 'name',
112
                        'dbColumn'     => 'name',
114
                        'type'         => 'varchar',
113
                        'type'         => 'varchar',
115
                        'size'         => 16,
114
                        'size'         => 16,
116
                        'required'     => true
115
                        'required'     => true,
-
 
116
                        'unique'       => true
117
                        ),
117
                        ),
118
                  'description'  => array(
118
                  'description'  => array(
119
                        'dbColumn'     => 'description',
119
                        'dbColumn'     => 'description',
120
                        'type'         => 'varchar',
120
                        'type'         => 'varchar',
121
                        'size'         => 64,
121
                        'size'         => 64,
Line 163... Line 163...
163
               )
163
               )
164
            ) // users permissions view
164
            ) // users permissions view
165
         );
165
         );
166
         $this->configuration = array_merge_recursive( $this->configuration, $permissions );
166
         $this->configuration = array_merge_recursive( $this->configuration, $permissions );
167
         
167
         
168
         parent::__construct( $dbConnection, $dbDef, $dbLoginInfo );
168
         parent::__construct( $connection, $customFields );
169
         
169
         
170
      } // constructor
170
   } // constructor
171
      
171
      
172
      /**
172
   /**
173
       * Function will build tables when called
173
    * Function will build tables when called
174
       * 
174
    * 
175
       * Calls parent::buildTable first, then creates a view. The query
175
    * Calls parent::buildTable first, then creates a view. The query
176
       * for the view is created using sprintf so we can allow the developers
176
    * for the view is created using sprintf so we can allow the developers
177
       * to modify the table names, etc...
177
    * to modify the table names, etc...
178
       * 
178
    * 
179
       */
179
    */
180
      public function buildTable() {
180
   public function buildTable() {
181
         parent::buildTable();
181
      parent::buildTable();
182
         // add a unique constraint on users permissions for user id and permission id
182
      // add a unique constraint on users permissions for user id and permission id
183
         // we do it this way since there is no way to do it automagically
183
      // we do it this way since there is no way to do it automagically
184
         // without rewriting the $configuration code
184
      // without rewriting the $configuration code
185
         $query = sprintf( 'alter table %s add unique key (%s,%s)',
185
      $query = sprintf( 'alter table %s add unique key (%s,%s)',
186
            $this->configuration['tables']['users permissions']['table'],
186
         $this->configuration['tables']['users permissions']['table'],
187
            $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
187
         $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
188
            $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn']
188
         $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn']
189
            );
189
         );
190
         $this->doSQL( $query );
190
      $this->doSQL( $query );
191
         // create a convenience view
191
      // create a convenience view
192
         $query = sprintf( "create or replace view %s as 
192
      $query = sprintf( "create or replace view %s as 
193
            select 
193
         select 
194
               %s.%s users_id,      /* users id */
194
            %s.%s users_id,      /* users id */
195
               %s.%s user,          /* users login */
195
            %s.%s user,          /* users login */
196
               %s.%s permission_id, /* permissions id */
196
            %s.%s permission_id, /* permissions id */
197
               %s.%s permission,    /* permissions name */
197
            %s.%s permission,    /* permissions name */
198
               %s.%s description,   /* permissions description */
198
            %s.%s description,   /* permissions description */
199
               %s.%s category,      /* permissions_categories name */
199
            %s.%s category,      /* permissions_categories name */
200
               ifnull(%s.%s,0)  value /* actual value */
200
            ifnull(%s.%s,0)  value /* actual value */
201
            from 
201
         from 
202
               %s /* users */
202
            %s /* users */
203
               join %s   /* permissions (permissions_id */
203
            join %s   /* permissions (permissions_id */
204
               left join %s using (%s,%s)   /* users_permissions (users_id) */
204
            left join %s using (%s,%s)   /* users_permissions (users_id) */
205
               join %s using (%s)   /* permissions_categories( permissions_categories_id) */",
205
            join %s using (%s)   /* permissions_categories( permissions_categories_id) */",
206
            $this->configuration['views']['users permissions']['name'],
206
         $this->configuration['views']['users permissions']['name'],
207
            $this->configuration['tables']['users']['table'],
207
         $this->configuration['tables']['users']['table'],
208
            $this->configuration['tables']['users']['id'],
208
         $this->configuration['tables']['users']['id'],
209
            $this->configuration['tables']['users']['table'],
209
         $this->configuration['tables']['users']['table'],
210
            $this->configuration['tables']['users']['fields']['login']['dbColumn'],
210
         $this->configuration['tables']['users']['fields']['login']['dbColumn'],
211
            $this->configuration['tables']['permissions']['table'],
211
         $this->configuration['tables']['permissions']['table'],
212
            $this->configuration['tables']['permissions']['id'],
212
         $this->configuration['tables']['permissions']['id'],
213
            $this->configuration['tables']['permissions']['table'],
213
         $this->configuration['tables']['permissions']['table'],
214
            $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
214
         $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
215
            $this->configuration['tables']['permissions']['table'],
215
         $this->configuration['tables']['permissions']['table'],
216
            $this->configuration['tables']['permissions']['fields']['description']['dbColumn'],
216
         $this->configuration['tables']['permissions']['fields']['description']['dbColumn'],
217
            $this->configuration['tables']['permissions categories']['table'],
217
         $this->configuration['tables']['permissions categories']['table'],
218
            $this->configuration['tables']['permissions categories']['fields']['name']['dbColumn'],
218
         $this->configuration['tables']['permissions categories']['fields']['name']['dbColumn'],
219
            $this->configuration['tables']['users permissions']['table'],
219
         $this->configuration['tables']['users permissions']['table'],
220
            $this->configuration['tables']['users permissions']['fields']['value']['dbColumn'],
220
         $this->configuration['tables']['users permissions']['fields']['value']['dbColumn'],
221
            $this->configuration['tables']['users']['table'],
221
         $this->configuration['tables']['users']['table'],
222
            $this->configuration['tables']['permissions']['table'],
222
         $this->configuration['tables']['permissions']['table'],
223
            $this->configuration['tables']['users permissions']['table'],
223
         $this->configuration['tables']['users permissions']['table'],
224
            $this->configuration['tables']['users']['id'],
224
         $this->configuration['tables']['users']['id'],
225
            $this->configuration['tables']['permissions']['id'],
225
         $this->configuration['tables']['permissions']['id'],
226
            $this->configuration['tables']['permissions categories']['table'],
226
         $this->configuration['tables']['permissions categories']['table'],
227
            $this->configuration['tables']['permissions categories']['id']
227
         $this->configuration['tables']['permissions categories']['id']
228
          );
228
       );
229
         $this->doSQL( $query );
229
      $this->doSQL( $query );
230
      }
230
   }
231
      
231
 
232
      /**
232
   /**
233
       * Adds a permission to the permissions table
233
    * Adds a permission to the permissions table
234
       * 
234
    * 
235
       * This simply adds a permission to the permissions table, adding the 
235
    * This simply adds a permission to the permissions table, adding the 
236
       * category if it doesn't exist already.
236
    * category if it doesn't exist already.
237
       * 
237
    * 
238
       * @parameter string $category Category to place the permission into
238
    * @parameter string $category Category to place the permission into
239
       * @parameter string $name The short name of the permission
239
    * @parameter string $name The short name of the permission
240
       * @parameter string $description The long (display) name for the permission
240
    * @parameter string $description The long (display) name for the permission
241
       * @parameter boolean $defaultValue The default value for the permission
241
    * @parameter boolean $defaultValue The default value for the permission
242
       */
242
    */
243
      public function addPermission ( $category, $name, $description, $defaultValue = 0 ) {
243
   public function addPermission ( $category, $name, $description, $defaultValue = 0 ) {
-
 
244
      $categoryID = $this->getAField( 
-
 
245
            $this->tableColumnName ( 'permissions categories' ),
-
 
246
            $this->tableColumnName ( 'permissions categories', 'id' ),
-
 
247
            $this->tableColumnName ( 'permissions categories', 'name' ),
-
 
248
            $category
-
 
249
         );
-
 
250
      if ( ! $categoryID ) { // we did not find the category, so add it
-
 
251
         $this->doSQL( sprintf( "insert into %s ( %s ) values ( %s )",
-
 
252
            $this->tableColumnName ( 'permissions categories' ),
-
 
253
            $this->tableColumnName ( 'permissions categories', 'name' ),
-
 
254
            $this->escapeString($category)
-
 
255
            )
-
 
256
         );
244
         $categoryID = $this->getAField( 
257
         $categoryID = $this->getAField( 
245
               $this->configuration['tables']['permissions categories']['table'],
258
            $this->tableColumnName ( 'permissions categories' ),
246
               $this->configuration['tables']['permissions categories']['id'],
259
            $this->tableColumnName ( 'permissions categories', 'id' ),
247
               $this->configuration['tables']['permissions categories']['fields']['name']['dbColumn'],
-
 
248
               $category
-
 
249
            );
-
 
250
         if ( ! $categoryID ) { // we did not find the category, so add it
-
 
251
            $this->doSQL( sprintf( "insert into %s ( %s ) values ( %s )",
-
 
252
               $this->configuration['tables']['permissions categories']['table'],
260
            $this->tableColumnName ( 'permissions categories', 'name' ),
253
               $this->configuration['tables']['permissions categories']['fields']['name']['dbColumn'],
-
 
254
               $this->escapeString($category)
-
 
255
               )
-
 
256
            );
-
 
257
            $categoryID = $this->getAField( 
-
 
258
                  $this->configuration['tables']['permissions categories']['table'],
-
 
259
                  $this->configuration['tables']['permissions categories']['id'],
-
 
260
                  $this->configuration['tables']['permissions categories']['fields']['name']['dbColumn'],
-
 
261
                  $category
261
            $category
262
               );
-
 
263
         } // if category not found
-
 
264
         $query = sprintf( "insert into %s ( %s,%s,%s,%s ) values ( %s,%s,%s,%s )",
-
 
265
               $this->configuration['tables']['permissions']['table'],
-
 
266
               $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
-
 
267
               $this->configuration['tables']['permissions']['fields']['description']['dbColumn'],
-
 
268
               $this->configuration['tables']['permissions']['fields']['permission category']['dbColumn'],
-
 
269
               $this->configuration['tables']['permissions']['fields']['default']['dbColumn'],
-
 
270
               $this->escapeString($name),
-
 
271
               $this->escapeString($description),
-
 
272
               $this->escapeString($categoryID),
-
 
273
               $defaultValue
-
 
274
         );
262
         );
275
         $this->doSQL( $query );
263
      } // if category not found
276
         $permissionsID = $this->getAField( 
264
      $query = sprintf( "insert into %s ( %s,%s,%s,%s ) values ( %s,%s,%s,%s )",
277
               $this->configuration['tables']['permissions']['table'],
265
            $this->tableColumnName ( 'permissions' ),
278
               $this->configuration['tables']['permissions']['id'],
266
            $this->tableColumnName ( 'permissions', 'name' ),
279
               $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
267
            $this->tableColumnName ( 'permissions', 'description' ),
280
               $this->escapeString($name)
-
 
281
            );
-
 
282
      } // addPermission
-
 
283
      
-
 
284
      /**
-
 
285
       * Sets any unset user permissions
-
 
286
       * 
-
 
287
       * This will set any missing permissions which have a true value in the
268
            $this->tableColumnName ( 'permissions', 'permission category' ),
288
       * users permissions join table. This will NOT modify any existing records
-
 
289
       * but will add new records if they do not exist in the table.
-
 
290
       * 
-
 
291
       * @parameters string $user_id If set, will limit to only one user (not implemented)
-
 
292
       */
-
 
293
      public function setUsersPermissions ( $user_id = null ) {
269
            $this->tableColumnName ( 'permissions', 'default' ),
294
         $query = sprintf(
270
            $this->escapeString($name),
295
            "insert into %s (%s,%s,%s)
271
            $this->escapeString($description),
296
            select
272
            $this->escapeString($categoryID),
297
               %s.%s,
-
 
298
               %s.%s,
273
            $defaultValue
299
               %s.%s
-
 
300
            from
274
      );
301
               %s
-
 
302
               join %s
-
 
303
            where
-
 
304
               %s.%s
-
 
305
               and not exists
-
 
306
                  (
-
 
307
                     select 1
275
      $this->doSQL( $query );
308
                     from %s
-
 
309
                     where
-
 
310
                        %s.%s = %s.%s
-
 
311
                        and %s.%s = %s.%s
276
      $permissionsID = $this->getAField( 
312
                  )",
-
 
313
            $this->configuration['tables']['users permissions']['table'],
-
 
314
            $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
-
 
315
            $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn'],
-
 
316
            $this->configuration['tables']['users permissions']['fields']['value']['dbColumn'],
-
 
317
            $this->configuration['tables']['users']['table'],
-
 
318
            $this->configuration['tables']['users']['id'],
-
 
319
            $this->configuration['tables']['permissions']['table'],
277
            $this->configuration['tables']['permissions']['table'],
320
            $this->configuration['tables']['permissions']['id'],
278
            $this->configuration['tables']['permissions']['id'],
-
 
279
            $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
-
 
280
            $this->escapeString($name)
-
 
281
         );
-
 
282
   } // addPermission
-
 
283
 
-
 
284
   /**
-
 
285
    * Sets any unset user permissions
-
 
286
    * 
-
 
287
    * This will set any missing permissions which have a true value in the
-
 
288
    * users permissions join table. This will NOT modify any existing records
-
 
289
    * but will add new records if they do not exist in the table.
-
 
290
    * 
-
 
291
    * @parameters string $user_id If set, will limit to only one user (not implemented)
-
 
292
    */
-
 
293
   public function setUsersPermissions ( $user_id = null ) {
-
 
294
      $query = sprintf(
-
 
295
         "insert into %s (%s,%s,%s)
-
 
296
         select
-
 
297
            %s.%s,
-
 
298
            %s.%s,
-
 
299
            %s.%s
-
 
300
         from
-
 
301
            %s
-
 
302
            join %s
-
 
303
         where
-
 
304
            %s.%s
-
 
305
            and not exists
-
 
306
               (
-
 
307
                  select 1
-
 
308
                  from %s
-
 
309
                  where
-
 
310
                     %s.%s = %s.%s
-
 
311
                     and %s.%s = %s.%s
-
 
312
               )",
-
 
313
         $this->configuration['tables']['users permissions']['table'],
-
 
314
         $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
-
 
315
         $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn'],
-
 
316
         $this->configuration['tables']['users permissions']['fields']['value']['dbColumn'],
-
 
317
         $this->configuration['tables']['users']['table'],
-
 
318
         $this->configuration['tables']['users']['id'],
-
 
319
         $this->configuration['tables']['permissions']['table'],
-
 
320
         $this->configuration['tables']['permissions']['id'],
321
            $this->configuration['tables']['permissions']['table'],
321
         $this->configuration['tables']['permissions']['table'],
322
            $this->configuration['tables']['permissions']['fields']['default']['dbColumn'],
322
         $this->configuration['tables']['permissions']['fields']['default']['dbColumn'],
323
            $this->configuration['tables']['users']['table'],
323
         $this->configuration['tables']['users']['table'],
324
            $this->configuration['tables']['permissions']['table'],
324
         $this->configuration['tables']['permissions']['table'],
325
            $this->configuration['tables']['permissions']['table'],
325
         $this->configuration['tables']['permissions']['table'],
326
            $this->configuration['tables']['permissions']['fields']['default']['dbColumn'],
326
         $this->configuration['tables']['permissions']['fields']['default']['dbColumn'],
327
            $this->configuration['tables']['users permissions']['table'],
327
         $this->configuration['tables']['users permissions']['table'],
328
            $this->configuration['tables']['users permissions']['table'],
328
         $this->configuration['tables']['users permissions']['table'],
329
            $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
329
         $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
330
            $this->configuration['tables']['users']['table'],
330
         $this->configuration['tables']['users']['table'],
331
            $this->configuration['tables']['users']['id'],
331
         $this->configuration['tables']['users']['id'],
332
            $this->configuration['tables']['users permissions']['table'],
332
         $this->configuration['tables']['users permissions']['table'],
333
            $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn'],
333
         $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn'],
334
            $this->configuration['tables']['permissions']['table'],
334
         $this->configuration['tables']['permissions']['table'],
335
            $this->configuration['tables']['permissions']['id']
335
         $this->configuration['tables']['permissions']['id']
336
            );
336
         );
337
            $this->doSQL($query, true );
337
         $this->doSQL($query, true );
338
      } // setUsersPermissions
338
   } // setUsersPermissions
339
      
339
 
340
      public function getARecord( $whereFields, $fieldList = null ) {
340
   public function getARecord( $whereFields, $fieldList = null ) {
341
         $id = isset( $whereFields['id'] ) ? $whereFields['id'] : null;
341
      $id = isset( $whereFields['id'] ) ? $whereFields['id'] : null;
342
         $result = parent::getARecord( $whereFields, $fieldList );
342
      $result = parent::getARecord( $whereFields, $fieldList );
343
         if ( $result && isset( $id ) ) {
343
      if ( $result && isset( $id ) ) {
344
            $result['permissions'] = $this->getPermissions( $id );
344
         $result['permissions'] = $this->getPermissions( $id );
345
         }
-
 
346
         return $result;
-
 
347
      }
345
      }
348
      
346
      return $result;
349
      
347
   }
-
 
348
 
-
 
349
 
350
      /**
350
   /**
351
       * Gets permissions for one user
351
    * Gets permissions for one user
352
       * 
352
    * 
353
       * NOTE: if $id is -1, indicating a new user, simply retrieves all
353
    * NOTE: if $id is -1, indicating a new user, simply retrieves all
354
       * permissions from the permissions table with the default value
354
    * permissions from the permissions table with the default value
355
       * 
355
    * 
356
       * WARNING: this uses the view, which is not as flexible as the
356
    * WARNING: this uses the view, which is not as flexible as the
357
       * tables as far as modifying programmatically
357
    * tables as far as modifying programmatically
358
       * 
358
    * 
359
       * The return value is an associative array, where the key is
359
    * The return value is an associative array, where the key is
360
       * the permission (short) name and the value is 0 or 1 (boolean
360
    * the permission (short) name and the value is 0 or 1 (boolean
361
       * true/false)
361
    * true/false)
362
       * 
362
    * 
363
       * @param integer $id the user id of the record we want.
363
    * @param integer $id the user id of the record we want.
364
       * 
364
    * 
365
       * @returns string[] Array of associative arrays with all data
365
    * @returns string[] Array of associative arrays with all data
366
       */
366
    */
367
      public function getPermissions ( $id ) {
367
   public function getPermissions ( $id ) {
368
         $query = '';
368
      $query = '';
369
         if ( $id == -1 ) {
369
      if ( $id == -1 ) {
370
            $query = "select name permission,default_value value from _permissions";
370
         $query = "select name permission,default_value value from _permissions";
371
         } else {
371
      } else {
372
            $query = "select permission,value from _view_users_permissions where users_id = $id";
372
         $query = "select permission,value from _view_users_permissions where users_id = $id";
373
         }
-
 
374
         $results = $this->doSQL( $query );
-
 
375
         $values = $results->fetch_all(MYSQLI_ASSOC);
-
 
376
         return array_column( $values, 'value', 'permission' );
-
 
377
      }
373
      }
-
 
374
      $results = $this->doSQL( $query );
-
 
375
      $values = $results->fetch_all(MYSQLI_ASSOC);
-
 
376
      return array_column( $values, 'value', 'permission' );
378
      
377
   }
-
 
378
 
379
      /**
379
   /**
380
       * Gets all permissions with names, group names, etc... from database
380
    * Gets all permissions with names, group names, etc... from database
381
       * 
381
    * 
382
       * NOTE: if $id is -1, indicating a new user, simply retrieves all
382
    * NOTE: if $id is -1, indicating a new user, simply retrieves all
383
       * permissions from the permissions table with the default value
383
    * permissions from the permissions table with the default value
384
       * 
384
    * 
385
       * WARNING: this uses the view, which is not as flexible as the
385
    * WARNING: this uses the view, which is not as flexible as the
386
       * tables as far as modifying programmatically
386
    * tables as far as modifying programmatically
387
       * 
387
    * 
388
       * @param integer $id the user id of the record we want.
388
    * @param integer $id the user id of the record we want.
389
       * @returns string[] Array of associative arrays with all data
389
    * @returns string[] Array of associative arrays with all data
390
       */
390
    */
391
      public function getFullPermissions( $id ) {
391
   public function getFullPermissions( $id ) {
392
         $query = '';
392
      $query = '';
393
         if ( $id == -1 ) {
393
      if ( $id == -1 ) {
394
            $query = "select _permission_id permission_id, _permissions.name permission, description, _permissions_categories.name category, default_value value from _permissions join _permissions_categories using (_permission_category_id)";
394
         $query = "select _permission_id permission_id, _permissions.name permission, description, _permissions_categories.name category, default_value value from _permissions join _permissions_categories using (_permission_category_id)";
395
         } else {
395
      } else {
396
            $query = "select permission_id,permission,description,category,value from _view_users_permissions where users_id = $id order by category,description";
396
         $query = "select permission_id,permission,description,category,value from _view_users_permissions where users_id = $id order by category,description";
397
         }
-
 
398
         $results = $this->doSQL( $query );
-
 
399
         return $results->fetch_all(MYSQLI_ASSOC);
-
 
400
      }
397
      }
-
 
398
      $results = $this->doSQL( $query );
-
 
399
      return $results->fetch_all(MYSQLI_ASSOC);
401
      
400
   }
-
 
401
 
402
      /**
402
   /**
403
       * Adds/Updates a group of permissions for a user
403
    * Adds/Updates a group of permissions for a user
404
       * 
404
    * 
405
       * @param integer $userID The user ID to update
405
    * @param integer $userID The user ID to update
406
       * @param string[] $newData An array where the key is the field name and the value is the new value to use
406
    * @param string[] $newData An array where the key is the field name and the value is the new value to use
407
       */
407
    */
408
      public function updatePermissions ( $userID, $newData ) {
408
   public function updatePermissions ( $userID, $newData ) {
409
         $query = '';
409
      $query = '';
410
         foreach ( $newData as $key => $value ) {
410
      foreach ( $newData as $key => $value ) {
411
            $query = sprintf(
411
         $query = sprintf(
412
                  "insert into %s 
412
               "insert into %s 
413
                     ( %s,%s,%s ) 
413
                  ( %s,%s,%s ) 
414
                     select %s, %s, %s 
414
                  select %s, %s, %s 
415
                     from %s
415
                  from %s
416
                     where %s = %s 
416
                  where %s = %s 
417
                  on duplicate key update %s = %s",
417
               on duplicate key update %s = %s",
418
               $this->configuration['tables']['users permissions']['table'],
418
            $this->tableColumnName ( 'users permissions' ),
419
               $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
419
            $this->tableColumnName ( 'users permissions', 'user_id' ),
420
               $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn'],
420
            $this->tableColumnName ( 'users permissions', 'permission_id' ),
421
               $this->configuration['tables']['users permissions']['fields']['value']['dbColumn'],
421
            $this->tableColumnName ( 'users permissions', 'value' ),
422
               $userID,
422
            $userID,
423
               $this->configuration['tables']['permissions']['id'],
423
            $this->tableColumnName ( 'permissions', 'id', true ),
424
               $value,
424
            $value,
425
               $this->configuration['tables']['table'],
425
            $this->tableColumnName ( 'permissions' ),
426
               $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
426
            $this->tableColumnName ( 'permissions', 'name' ),
427
               $this->excapeString($key),
427
            $this->escapeString($key),
428
               $this->configuration['tables']['users permissions']['fields']['value']['dbColumn'],
428
            $this->tableColumnName ( 'users permissions', 'value' ),
429
               $value
429
            $value
430
            );
430
         );
431
            //$query = "insert into _users_permissions ( _user_id,_permission_id,value) select $userID, _permission_id, $value from _permissions where name = '$key' on duplicate key update value = $value";
-
 
432
            $this->doSQL( $query, 'In updatePermissions' );
431
         $this->doSQL( $query );
433
         }
432
      }
434
      } // update
433
   } // update
435
 
434
 
436
}
435
}
437
 
436
 
438
?>
437
?>