It took a bit of time to figure out as most of the examples in the forum were based on version 5.
Here is what I ended up doing for Sugar version 6 in case any one is interested:
In the logic hook I set-up my code to be run after login.
/custom/modules/Users/logic_hooks.php
PHP Code:
<?php
// Do not store anything in this file that is not part of the array or the hook version. This file will
// be automatically rebuilt in the future.
$hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['after_login'] = Array();
$hook_array['after_login'][] = Array(1, 'SugarFeed old feed entry remover', 'modules/SugarFeed/SugarFeedFlush.php','SugarFeedFlush', 'flushStaleEntries');
// Add to the default role
$hook_array['after_login'][] = Array(2, 'Add to default user role', 'custom/modules/Users/AddToDefaultUserRole.php','AddToDefaultUserRole', 'addToDefaultUserRole');
?>
In the custom code, I check if the user is part of a given role, and if not, add him/her to the role. The only ugly bit about this code is that the ACLRole ID is hard-coded. I want to find a way of making it configurable. Unfortunately ACLRole cannot be extended from studio, otherwise I would have added an additional boolean field (Default), that would be read in the script below.
/custom/modules/Users/AddToDefaultUserRole.php
PHP Code:
<?php
class AddToDefaultUserRole {
/* Unique ID of the default user role that all users must be a member of. */
const DEFAULT_USER_ROLE_ID = "ca4a4302-a96b-d9c5-f949-4ee88a48b047";
function addToDefaultUserRole(&$bean, $event, $arguments) {
// Check that the bean is a User class
if (method_exists($bean, 'check_role_membership')) {
// Fetch the default role
$role = new ACLRole();
$role->retrieve(AddToDefaultUserRole::DEFAULT_USER_ROLE_ID);
// Check if the user is already a member of the default role
$in_role = $bean->check_role_membership($role->name);
if (!$in_role) {
// Add user to role, if he/she is not already a member
$role->set_relationship('acl_roles_users',
array('role_id' => $role->id, 'user_id' => $bean->id),
false);
}
}
}
}
?>
Bookmarks