Globally updating user Access Level permissions

11 posts by 3 authors in: Forums > CMS Builder
Last Post: December 9, 2013   (RSS)

By gkornbluth - November 25, 2013 - edited: December 5, 2013

Hi All,

I’ve imported a great many user records, and now find myself needing to globally change all user’s access level to 6 “author”, for an existing multi-record table called listing.

This is a pretty scary concept to me, so I thought I’d pass the question on to any of you who are more expert in these matters.

I'm using the Website Membership Plugin (v1.10) and there’s some code in the user sign-up.php file that is supposed to reset account access but I’m not sure if it’s appropriate, or how to implement it for this purpose (where to put a foreach loop or other code to cycle through all existing user records, for example.)

I hope someone more knowledgeable then me is willing to share.

Here's that code snippet modified as best I can:

   // NOTE: You can repeat this block to grant access to multiple sections
        mysql_insert('_accesslist', array(
          'userNum'      => $userNum,
          'tableName'    => 'all',   // insert tablename you want to grant access to, or 'all' for all sections
          'accessLevel'  => '1',         // access level allowed: 0=none, 6=author, 9=editor
          //'maxRecords'   => '',          // max listings allowed (leave blank for unlimited)
          'randomSaveId' => '123456789', // ignore - for internal use
        ), true);
        
        // NOTE: You can repeat this block to grant access to multiple sections
        mysql_insert('_accesslist', array(
          'userNum'      => $userNum,
          'tableName'    => 'listings',   // insert tablename you want to grant access to, or 'all' for all sections
          'accessLevel'  => '6',         // access level allowed: 0=none, 6=author, 9=editor
          //'maxRecords'   => '',          // max listings allowed (leave blank for unlimited)
          'randomSaveId' => '123456789', // ignore - for internal use
        ), true);

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Daryl - November 28, 2013

Hi Jerry,

What I would do is I will create a one-time script that I will execute once after I've imported all the users. The script will load all the users, except the admins, and loop through each record with your code above.

Hope this helps!

Cheers!

Daryl Maximo
PHP Programmer - interactivetools.com

By gkornbluth - November 28, 2013 - edited: November 28, 2013

Sounds like a plan, Daryl.

Is the code above correct to accomplish my goal?

Jerry

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Daryl - November 28, 2013

Hey Jerry, your code looks good! I would loop through the first 10 records first though and see how it goes before I loop through everyone.

Cheers!

Daryl Maximo
PHP Programmer - interactivetools.com

Great Daryl,

I'll be testing it on my test server, so no worries about the record limit.

I look forward to seeing what you come up with (and how to accomplish this in the future)

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Daryl - November 29, 2013

Hi Jerry,

Can you please post the errors here?

And out of curiosity, are you intending to add a new user on each loop in this part of your code?

<?php    
  $colsToValues = array();
  $colsToValues['updatedDate=']     = 'NOW()';
  // $colsToValues['fullname']         = 'joe';
  $userNum = mysql_insert(accountsTable(), $colsToValues, true); ?>

If you just wanted to update the users, can you try if this code below will work?

<?php

$accountsRecords = mysql_select(accountsTable(), 'isAdmin != 1');
foreach($accountsRecords as $record){
  
  $userNum = $record['num'];
  
  mysql_insert('_accesslist', array(
    'userNum'      => $userNum,
    'tableName'    => 'all',   // insert tablename you want to grant access to, or 'all' for all sections
    'accessLevel'  => '1',         // access level allowed: 0=none, 6=author, 9=editor
    //'maxRecords'   => '',          // max listings allowed (leave blank for unlimited)
    'randomSaveId' => '123456789', // ignore - for internal use
  ), true);
  
  // NOTE: You can repeat this block to grant access to multiple sections
  mysql_insert('_accesslist', array(
    'userNum'      => $userNum,
    'tableName'    => 'listings',   // insert tablename you want to grant access to, or 'all' for all sections
    'accessLevel'  => '6',         // access level allowed: 0=none, 6=author, 9=editor
    //'maxRecords'   => '',          // max listings allowed (leave blank for unlimited)
    'randomSaveId' => '123456789', // ignore - for internal use
  ), true);
  
}
?>

Let me know if that works for you.

Thanks,

Daryl Maximo
PHP Programmer - interactivetools.com

As always, more testing... but the first results are extremely promising.

I think you're amazing.

And no, I was trying to update existing records only. I was just mucking about attempting a solution (which obviously failed).

Again, and again, I (and all of us mere mortal developers) are in your debt.

How do you juggle so many issues at one time and know so much?

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - November 29, 2013 - edited: December 5, 2013

Hi Daryl,

SORRY, PLEASE DISREGARD THIS POST. I THINK THE ERRORS ARE DUE TO CONFIGURATIONS ON THE NEW SERVER, SINCE I CAN"T DUPLICATE THEM ON ANY OTHER SERVER.

I thought that I was out of the woods, but after the update, which seemed to go fine, I got the following error  when trying to access the Created by pull down, where the pull down was supposed to appear. (there are about 2300 records in the listings section and about 1600 users)

by Fatal error: Allowed memory size of 100663296 bytes exhausted (tried to allocate 2097152 bytes) in /home/public_html/cmsAdmin/lib/common.php on line 889 ) (

I restored backups of both the access list and user accounts and tried it again with the same results. Then restored the backups again and left it that way (the pull down works with the backups restored).

I also found these in the error log:

[30-Nov-2013 01:34:23 UTC] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /home/public_html/cmsAdmin/lib/mysql_functions.php on line 22
[30-Nov-2013 01:35:55 UTC] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /home/public_html/cmsAdmin/lib/mysql_functions.php on line 230
[30-Nov-2013 01:39:10 UTC] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /home/public_html/cmsAdmin/lib/mysql_functions.php on line 228
[30-Nov-2013 01:41:21 UTC] PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /home/public_html/cmsAdmin/lib/mysql_functions.php on line 69

I asked the Server admin to increase the max_execution_time and he did, and the above errors did not recur, but I'm still getting the same notice on the access update page as before :

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Any suggestions?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - December 8, 2013

Hi Jerry, 

My first guess is that you have so many users that PHP is running out of memory trying to put them all in a list for the "Created By" pulldown.  If you're running up against a CPU or memory limit for that feature there's no easy solution.  Even as a pulldown list it's only really practical for several hundred users before it gets hard to filter through.  The only option is really to use another method to change the "createdByUserNum" value.  On our end we'll need to write an alternative user selection widget for the future that lets you search and page through results.  (I'll have to give that some more though as to how it could be best accomplished) or maybe an autocomplete field. 

Dave Edis - Senior Developer
interactivetools.com