Change user when isn't administrator

Hello Everyone!
How can I prevent that a user creating an account and he assigning it to another user (without administrator permissions)?
Thanks

Hey,

I’m not sure if there are options for control at that level, out of the box

However, it sounds like something that’d be possible with custom changes.

I suppose it depends on how much administrator-level verification you’d like.

For example, the following Logic Hook would prevent non-admin users from changing the “Assigned User” value.
(Both on Edit and Create views)

class keepAssigneeC{
    public function keepAssigneeF(SugarBean $bean, $event, $arguments){
        global $current_user;       

        $user = BeanFactory::getBean('Users', $current_user->id);

//If user is not an Admin, and a new record is being created
        if (!($user->is_admin) && $bean->fetched_row["id"]==""){            
 //Keep the Assigned user as Current user
            $bean->assigned_user_id = $current_user->id;
        }         
// Else, if User is not Admin, and an existing record is being edited
        elseif (!($user->is_admin) && $bean->fetched_row["assigned_user_id"]!=$bean->assigned_user_id)
       {
 //Set the assigned user back to previous value.
           $bean->assigned_user_id = $bean->fetched_row["assigned_user_id"];
       }
    }
}

I’ve written some posts recently on setting up Logic Hooks, such as:

But let me know if you have any questions :slight_smile:


Or, are you looking for something more advanced?
What sort of flow are you imagining?

I hope I have been clear in explaining my problem. :hugs:
If it’ll help, I can make you an explanatory video.
Could you just explain where to insert this code snippet,please?
Thanks a lot

Hey,

The linked thread above should hopefully show you the format of where to add the code, but it should be as follows:

Create a new file at:
/custom/Extensions/modules/Accounts/Ext/Logichooks/

(if this directory does not exist, feel free to create it)

With content like the following:

<?php
$hook_array['before_save'][] = Array(1, 'Dont allow editing of Assignee', 'custom/modules/Accounts/keepAssignee.php','keepAssigneeC', 'keepAssigneeF');

(Feel free to rename any files/functions/classes )

Then, in the location specified in the Logic Hook line created above, create a file with the same name
(ie: in custom/modules/Accounts/ location, i created ‘keepAssignee.php’)

In this file, I added the code snippet posted in my first reply


Then, in the CRM Run a Quick Repair and Rebuild
Just to note, the code doesn't fully do what you have requested above But it should prevent non-admin users from changing the “Assigned User” value.

Let me know if you have any issues with the above

Thank you, but it doesn’t work.
I still don’t know why that didn’t work out.

Hi.
/custom/Extensions/Accounts/Ext/Logichooks/ or

/custom/Extension/modules/Accounts/Ext/LogicHooks

lowercase or uppercase …

Thanks

Hey,

Is this for the first step in the above?

ie:

Create a new file at:
/custom/Extensions/Accounts/Ext/Logichooks/

If so, apologies, you’re right, the file should be created at:
/custom/Extension/modules/Accounts/Ext/LogicHooks

I’ll correct the above post.

thanks.
But not working :

Error:
class keepAssigneeC{ public function keepAssigneeF(SugarBean $bean, $event, $arguments){ global $current_user; $user = BeanFactory::getBean(‘Users’, $current_user->id); //If user is not an Admin, and a new record is being created if (!($user->is_admin) && $bean->fetched_row[“id”]==""){ //Keep the Assigned user as Current user $bean->assigned_user_id = $current_user->id; } // Else, if User is not Admin, and an existing record is being edited elseif (!($user->is_admin) && $bean->fetched_row[“assigned_user_id”]!=$bean->assigned_user_id) { //Set the assigned user back to previous value. $bean->assigned_user_id = $bean->fetched_row[“assigned_user_id”]; } } }
Fatal error: Uncaught Error: Class ‘keepAssigneeC’ not found in /home/customer/www//public_html/include/utils/LogicHook.php:270 Stack trace: #0 /home/customer/www//public_html/include/utils/LogicHook.php(208): LogicHook->process_hooks(Array, ‘before_save’, Array) #1 /home/customer/www//public_html/data/SugarBean.php(3112): LogicHook->call_custom_logic(‘Accounts’, ‘before_save’, Array) #2 /home/customer/www//public_html/data/SugarBean.php(2376): SugarBean->call_custom_logic(‘before_save’, Array) #3 /home/customer/www//public_html/include/SugarObjects/templates/company/Company.php(119): SugarBean->save(false) #4 /home/customer/www//public_html/modules/Accounts/AccountFormBase.php(511): Company->save(false) #5 /home/customer/www//public_html/modules/Accounts/Save.php(55): AccountFormBase->handleSave(’’, true, false) #6 /home/customer/www//public_html/include/M in /home/customer/www/***********************/public_html/include/utils/LogicHook.php on line 270

Hi,

Hmmm,
Would you be able to pass over a screenshot of the file(s) you’ve created?

(Especially the content of the “keepAssignee.php” file that seems to be generating the error:
Fatal error: Uncaught Error: Class ‘keepAssigneeC’ not found in …)

Here is a screenshot:

Thanks a lot!

Hey,

Thanks for those screenshots!

In the lower keepAssignee.php file, it looks like its missing a “<?php” tag at the start
image

As its a php file, it will need those to be picked up as PHP


Would you be able to add "<?php" at the top of the file? (Similar to the upper text file in the screenshot)

So that it becomes:

<?php
class keepAssigneeC{
    public function keepAssigneeF(SugarBean $bean, $event, $arguments){

Then, repair and rebuild in the CRM Can you confirm if this allows the code to run as expected?

I’m glad that it finally works.
If I had to block a specific user, what can I do? …. then I’m done.
Great. Thanks a lot

I tried something like it:

but not working.

Hey,

$current_user->name == "xxxx" 

Should indeed work

However, the value you’ve blanked out seems quite short, and I think “name” could be the full name (First+Last) of the user

Have you entered the full name or just the username?

Another method might be to use the ID of the user, as that’d be unique to each user
You can grab the User’s ID either from the Database, or from the URL when viewing the User’s profile
eg:
image
would be:

$current_user->id== "4c727b9e-529f-d669-5d94-5f7733d23292" 

Thanks. I owe you one. :wink:

1 Like