Subversion Repositories php_users

Rev

Rev 18 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 20
Line 66... Line 66...
66
 */
66
 */
67
 
67
 
68
class usersPermissionsDataSource extends usersDataSource {
68
class usersPermissionsDataSource extends usersDataSource {
69
   
69
   
70
      /**
70
      /**
71
       * constructor for an instance of the class
71
       * extends usersDataSource
72
       * 
72
       * 
73
       * If $dbConnection is not null, will be used for database access
73
       * Adds the new fields needed for permissions. First, creates array
74
       * If $dbLoginInfo is not null, will override $dbConnection, make
74
       * $permissions, then does a recursive merge into 
75
       * a new connection and use that.
75
       * usersDataSource::$configuration. 
76
       * 
76
       * NOTE: we must merge this before we merge in $dbDef so the
77
       * If $dbDef is set, will be merged with $configuration
77
       * developer can modify permissions also
78
       * 
78
       * 
79
       * @param mysqli $dbConnection Existing mysqli database connection
79
       * @param mysqli $dbConnection Existing mysqli database connection
80
       * @param string[] $dbDef Array to be merged with $configuration
80
       * @param string[] $dbDef Array to be merged with $configuration
81
       * @param string[] $dbLoginInfo Array containing username, hostname, etc.. to make mysqli connection_aborted
81
       * @param string[] $dbLoginInfo Array containing username, hostname, etc.. to make mysqli connection_aborted
82
       * 
82
       * 
83
       * @return null
83
       * @return null
84
       * 
84
       * 
85
       */
85
       */
86
      public function __construct( $dbConnection = null, $dbDef = array(), $dbLoginInfo = array() ) {
86
      public function __construct( $dbConnection = null, $dbDef = array(), $dbLoginInfo = array() ) {
87
         
87
         
-
 
88
      $permissions = array(
-
 
89
         'tables' => array(
88
      $this->configuration['tables']['permissions categories'] = array(
90
            'permissions categories' => array(
89
            'table'     => '_permissions_categories',   // table name for user records
91
               'table'     => '_permissions_categories',   // table name for user records
90
            'id'        => '_permission_category_id', // ID column name
92
               'id'        => '_permission_category_id', // ID column name
91
            'display'   => array(      // fields which are displayed to select
93
               'display'   => array(      // fields which are displayed to select
92
               'description'
94
                  'description'
93
               ),         
95
                  ),         
94
            'fields' => array(
96
               'fields' => array(
95
               'name'  => array(
97
                  'name'  => array(
96
                     'dbColumn'     => 'name',
98
                        'dbColumn'     => 'name',
97
                     'type'         => 'varchar',
99
                        'type'         => 'varchar',
98
                     'size'         => 16,
100
                        'size'         => 16,
99
                     'required'     => true
101
                        'required'     => true
100
                     )
102
                        )
101
            )
103
                  ),
102
         ); // adding permissions table
104
               ), // permissions categories table
103
 
-
 
104
      $this->configuration['tables']['permissions'] = array(
105
            'permissions' => array(
105
            'table'     => '_permissions',   // table name for user records
106
               'table'     => '_permissions',   // table name for user records
106
            'id'        => '_permission_id', // ID column name
107
               'id'        => '_permission_id', // ID column name
107
            'display'   => array(      // fields which are displayed to select
108
               'display'   => array(      // fields which are displayed to select
108
               'description'
109
                  'description'
109
               ),         
110
                  ),         
110
            'fields' => array(
111
               'fields' => array(
111
               'name'  => array(
112
                  'name'  => array(
112
                     'dbColumn'     => 'name',
113
                        'dbColumn'     => 'name',
113
                     'type'         => 'varchar',
114
                        'type'         => 'varchar',
114
                     'size'         => 16,
115
                        'size'         => 16,
115
                     'required'     => true
116
                        'required'     => true
116
                     ),
117
                        ),
117
               'description'  => array(
118
                  'description'  => array(
118
                     'dbColumn'     => 'description',
119
                        'dbColumn'     => 'description',
119
                     'type'         => 'varchar',
120
                        'type'         => 'varchar',
120
                     'size'         => 64,
121
                        'size'         => 64,
121
                     'required'     => true
122
                        'required'     => true
122
                     ),
123
                        ),
123
               'permission category' => array(
124
                  'permission category' => array(
124
                     'dbColumn'     => '_permission_category_id',
125
                        'dbColumn'     => '_permission_category_id',
125
                     'type'         => 'int unsigned',
126
                        'type'         => 'int unsigned',
126
                     'required'     => true,
127
                        'required'     => true,
127
                     ),
128
                        ),
128
               'default'  => array(
129
                  'default'  => array(
129
                     'dbColumn'     => 'default_value',
130
                        'dbColumn'     => 'default_value',
130
                     'type'         => 'boolean',
131
                        'type'         => 'boolean',
131
                     'required'     => true
132
                        'required'     => true
132
                     ),
133
                        ),
133
            )
134
               )
134
         ); // adding permissions table
135
            ), // permissions table
135
      
-
 
136
      $this->configuration['tables']['users permissions'] = array(
136
            'users permissions' => array(
137
            'table'     => '_users_permissions',   // table name for user records
137
               'table'     => '_users_permissions',   // table name for user records
138
            'id'        => '_user_permission_id', // ID column name
138
               'id'        => '_user_permission_id', // ID column name
139
            'fields' => array(
139
               'fields' => array(
140
               'user_id'  => array(
140
                  'user_id'  => array(
141
                     'dbColumn'     => '_user_id',
141
                        'dbColumn'     => '_user_id',
142
                     'type'         => 'int unsigned',
142
                        'type'         => 'int unsigned',
143
                     'required'     => true,
143
                        'required'     => true,
144
                     'references'   => 'users',
144
                        'references'   => 'users',
145
                     ),
145
                        ),
146
               'permission_id'  => array(
146
                  'permission_id'  => array(
147
                     'dbColumn'     => '_permission_id',
147
                        'dbColumn'     => '_permission_id',
148
                     'type'         => 'int unsigned',
148
                        'type'         => 'int unsigned',
149
                     'required'     => true,
149
                        'required'     => true,
150
                     'references'   => 'permissions',
150
                        'references'   => 'permissions',
151
                     ),
151
                        ),
152
               'value'  => array(
152
                  'value'  => array(
153
                     'dbColumn'     => 'value',
153
                        'dbColumn'     => 'value',
154
                     'type'         => 'boolean',
154
                        'type'         => 'boolean',
155
                     'required'     => true,
155
                        'required'     => true,
156
                     ),
156
                        ),
157
            )
157
               )
158
         ); // adding permissions table
158
            ) // users permissions table
-
 
159
         ),
-
 
160
         'views' => array(
-
 
161
            'users permissions' => array( 
-
 
162
               'name' => '_view_users_permissions' 
-
 
163
               )
-
 
164
            ) // users permissions view
-
 
165
         );
159
         $this->configuration['views']['users permissions'] = array( 'name' => '_view_users_permissions' );
166
         $this->configuration = array_merge_recursive( $this->configuration, $permissions );
160
         
167
         
161
         parent::__construct( $dbConnection, $dbDef, $dbLoginInfo );
168
         parent::__construct( $dbConnection, $dbDef, $dbLoginInfo );
162
         
169
         
163
      } // constructor
170
      } // constructor
164
      
171
      
Line 171... Line 178...
171
       * 
178
       * 
172
       */
179
       */
173
      public function buildTable() {
180
      public function buildTable() {
174
         parent::buildTable();
181
         parent::buildTable();
175
         // 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
-
 
184
         // without rewriting the $configuration code
176
         $query = sprintf( 'alter table %s add unique key (%s,%s)',
185
         $query = sprintf( 'alter table %s add unique key (%s,%s)',
177
            $this->configuration['tables']['users permissions']['table'],
186
            $this->configuration['tables']['users permissions']['table'],
178
            $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
187
            $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
179
            $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn']
188
            $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn']
180
            );
189
            );
Line 268... Line 277...
268
               $this->configuration['tables']['permissions']['table'],
277
               $this->configuration['tables']['permissions']['table'],
269
               $this->configuration['tables']['permissions']['id'],
278
               $this->configuration['tables']['permissions']['id'],
270
               $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
279
               $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
271
               $this->escapeString($name)
280
               $this->escapeString($name)
272
            );
281
            );
273
         //insert into _users_permissions (_user_id,_permission_id,value) select _user_id,$permissionsID,$defaultValue from _users
-
 
274
      } // addPermission
282
      } // addPermission
275
      
283
      
276
      /**
284
      /**
277
       * Sets any unset user permissions
285
       * Sets any unset user permissions
278
       * 
286
       * 
Line 336... Line 344...
336
            $result['permissions'] = $this->getPermissions( $id );
344
            $result['permissions'] = $this->getPermissions( $id );
337
         }
345
         }
338
         return $result;
346
         return $result;
339
      }
347
      }
340
      
348
      
-
 
349
      
-
 
350
      /**
-
 
351
       * Gets permissions for one user
-
 
352
       * 
-
 
353
       * NOTE: if $id is -1, indicating a new user, simply retrieves all
-
 
354
       * permissions from the permissions table with the default value
-
 
355
       * 
-
 
356
       * WARNING: this uses the view, which is not as flexible as the
-
 
357
       * tables as far as modifying programmatically
-
 
358
       * 
-
 
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
-
 
361
       * true/false)
-
 
362
       * 
-
 
363
       * @param integer $id the user id of the record we want.
-
 
364
       * 
-
 
365
       * @returns string[] Array of associative arrays with all data
-
 
366
       */
341
      public function getPermissions ( $id ) {
367
      public function getPermissions ( $id ) {
342
         $query = ( 
368
         $query = '';
343
            $id == -1 ?
369
         if ( $id == -1 ) {
344
               "select name permission,default_value value from _permissions" :
370
            $query = "select name permission,default_value value from _permissions";
-
 
371
         } else {
345
               "select permission,value from _view_users_permissions where users_id = $id"
372
            $query = "select permission,value from _view_users_permissions where users_id = $id";
346
         );
373
         }
347
         $results = $this->doSQL( $query );
374
         $results = $this->doSQL( $query );
348
         $values = $results->fetch_all(MYSQLI_ASSOC);
375
         $values = $results->fetch_all(MYSQLI_ASSOC);
349
         return array_column( $values, 'value', 'permission' );
376
         return array_column( $values, 'value', 'permission' );
350
      }
377
      }
351
       
378
      
-
 
379
      /**
-
 
380
       * Gets all permissions with names, group names, etc... from database
-
 
381
       * 
-
 
382
       * NOTE: if $id is -1, indicating a new user, simply retrieves all
-
 
383
       * permissions from the permissions table with the default value
-
 
384
       * 
-
 
385
       * WARNING: this uses the view, which is not as flexible as the
-
 
386
       * tables as far as modifying programmatically
-
 
387
       * 
-
 
388
       * @param integer $id the user id of the record we want.
-
 
389
       * @returns string[] Array of associative arrays with all data
-
 
390
       */
352
      public function getFullPermissions( $id ) {
391
      public function getFullPermissions( $id ) {
353
         $query = (
392
         $query = '';
354
            $id == -1 ?
393
         if ( $id == -1 ) {
355
               "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 {
356
               "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";
357
         );
397
         }
358
         $results = $this->doSQL( $query );
398
         $results = $this->doSQL( $query );
359
         return $results->fetch_all(MYSQLI_ASSOC);
399
         return $results->fetch_all(MYSQLI_ASSOC);
360
      }
400
      }
361
      
401
      
-
 
402
      /**
-
 
403
       * Adds/Updates a group of permissions for a user
-
 
404
       * 
-
 
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
-
 
407
       */
362
      public function updatePermissions ( $userID,$newData ) {
408
      public function updatePermissions ( $userID, $newData ) {
363
         $query = '';
409
         $query = '';
364
         foreach ( $newData as $key => $value ) {
410
         foreach ( $newData as $key => $value ) {
-
 
411
            $query = sprintf(
-
 
412
                  "insert into %s 
-
 
413
                     ( %s,%s,%s ) 
-
 
414
                     select %s, %s, %s 
-
 
415
                     from %s
-
 
416
                     where %s = %s 
-
 
417
                  on duplicate key update %s = %s",
-
 
418
               $this->configuration['tables']['users permissions']['table'],
-
 
419
               $this->configuration['tables']['users permissions']['fields']['user_id']['dbColumn'],
-
 
420
               $this->configuration['tables']['users permissions']['fields']['permission_id']['dbColumn'],
-
 
421
               $this->configuration['tables']['users permissions']['fields']['value']['dbColumn'],
-
 
422
               $userID,
-
 
423
               $this->configuration['tables']['permissions']['id'],
-
 
424
               $value,
-
 
425
               $this->configuration['tables']['table'],
-
 
426
               $this->configuration['tables']['permissions']['fields']['name']['dbColumn'],
-
 
427
               $this->excapeString($key),
-
 
428
               $this->configuration['tables']['users permissions']['fields']['value']['dbColumn'],
-
 
429
               $value
-
 
430
            );
365
            $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";
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";
366
            $this->doSQL( $query, 'In updatePermissions' );
432
            $this->doSQL( $query, 'In updatePermissions' );
367
         }
433
         }
368
      } // update
434
      } // update
369
 
435
 
370
}
436
}