Limit User Creation

Hi,

Is there any way in which I can limit the number of users an administrator can create. I guess I have to code it out but it would be nice if you can give a start on it.

Hi sid,

Please see this forum topic.

Thanks,

Will.

Hi Will,

Thanks for the quick response. I’ve seen that but am not able to query the database. How do I go about doing that?

There are plenty of resources online about how to query the database from the code in sugar but it can be quite overwhelimg if you are just starting out.

See if these two links are of any help:

How to use the bean instead of sql

Actual sql query’s

1 Like

Thanks Andy,

Will look into it.

Andy,

Both the articles were of help. I have retrieved the number of users stored in the database and matched with the number of max users a person can create and based on this condition if have restricted the display using $this->ev->display($this->showTitle). Now the problem I face is if the administrator has already reached the max no. of users he’s entitled to create, the edit view form also does not open restricting him from editing users.

How do I go about solving this? Please help.

Let me see your code?

I have attached the view.edit.php file in /custom/modules/Users/views/

The code is from line #225 to #234

Try zipping the file then attaching it.

Done. Please check above post.

You are going about this the wrong way. That is never going to work!

I dont really understand what your are trying to do?

So you want indvidual administrators to be able to add 5 users each or do you want a maximum of 5 users on the system?

A maximum of 5 users on the system.

Feeww thats simpler :wink:

What you want to do is add a before save logic hook on users that counts the number of users as you did in your code but prevents the user from saving if the number of users is above 5. You will have to stop the script with sugar_die() with an error message or use SugarApplication::redirect() to send the user to another page. Don’t forget to exit; after the redirect.

Here is a link to adding a logic hook: http://archive.h2ik.co/2011/02/logic-hooks-in-sugarcrm/

A better example: http://cheleguanaco.blogspot.co.uk/2009/06/simple-sugarcrm-logic-hook-example.html

Ok since this was easy I did this for you:

In this file : custom/modules/Users/logic_hooks.php

add these lines:


$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'prevent users', 'custom/modules/Users/maxusers.php','limit_users', 'limit_user_count');

create the file: custom/modules/Users/maxusers.php

and add this code:


class limit_users {
    function limit_user_count(&$bean, $event, $arguments)
    {
        $query = "SELECT COUNT(*) FROM users WHERE deleted = 0";
        $result = $GLOBALS['db']->getOne($query);

        //$GLOBALS['log']->fatal('no of users: '.$result);

        if($result >= 5){

           //redirct
            SugarApplication::appendErrorMessage('Action Prohibited: Maximum number of users created.');
                $params = array(
                'module'=> 'Users',
                'action'=>'ListView'
            );

            SugarApplication::redirect('index.php?' . http_build_query($params));

            exit;
        }
    }
}

Now if you try to add more than 5 users to the system it will redirect you back the user’s list view with an error message

you may have to do a repair and rebuild

And thats its…

1 Like

Thanks a lot Andy !! Didn’t expect you’ll code it out :slight_smile: Really appreciate the help. Thank you.

The only hitch which remains is that if the users equal the limit, I cant edit an already existing user as it uses the same form to create as well as edit, but the URLs are different.

Hence, the solution I’ve configured is to check the URL before saving. There’s a ‘stamp’ in the URL (I’ve googled it but not able to find its definition). What is that for? If I check the ‘stamp’ in the URL before saving will it solve my problem? Or it’s a wrong solution !!

There may be a better way. You could check if the record is newly created and only execute the code in the logic hook if it is newly created.

Wrap the code inside the function in this:

if (empty($this->bean->fetched_row['id'])) {
 // record is new
}

Thanks andy, will try it out.

Done!
I changed the code a bit. I checked the POST values of the form and found that $_POST[“record”] contains the ID if the record is edited and is blank if the record is new. So this is how I coded it out.

$query = “SELECT id FROM users WHERE deleted = 0”;
$result = $GLOBALS[‘db’]->query($query);

while($row = $GLOBALS[‘db’]->fetchByAssoc($result))
{
$id[] = $row[‘id’];
}

if($_POST[“record”] == NULL && count($id) >= 5){//checks if the entry is new and records are not more that 5
SugarApplication::appendErrorMessage(‘Action Prohibited: Maximum number of users created.’);
$params = array(
‘module’=> ‘Users’,
‘action’=>‘ListView’
);
SugarApplication::redirect(‘index.php?’ . http_build_query($params));
exit;
}