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

Hi Daryl,

After some perilous mucking on my test server, about I was able to come up with the code below, which inserted new records for each account that was not an admin.

When I tried to get the code to update existing accounts using mysql_update, I kept throwing errors and am lost as what to do to fix the issue.

Any help appreciated.

Jerry Kornbluth

<?php foreach ($accountsRecords as $record): ?>
<?php if(!$record['isAdmin']):?>
  <?php    
  $colsToValues = array();
  $colsToValues['updatedDate=']     = 'NOW()';
  // $colsToValues['fullname']         = 'joe';
  $userNum = mysql_insert(accountsTable(), $colsToValues, true); ?>
 <?php  // 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);
?>
<?php endif ?>
<?php endforeach ?>

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 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