This short tutorial explains how to change default ACL ownership field from “Assign To” to something else and how to have two ownership fields depending on user type in one module.
The problem:
This is the common business case. You have two types of users – for example, you have merchandisers and sales people that should both own particular accounts (i.e. convenient stores). Merchandisers should only view their accounts and sales representatives can view and edit theirs. User type information is stored in Users
module.
The solution:
First add two new Users relate fields to Accounts naming them: sales_rep_id and merchandiser_id. I also added new custom field to Users: custom_user_type. This field will determine the user as either sales rep or merchandiser.
Since SugarCRM by default only uses “Assign To” field for determining record ownership so you need to override SugarBeans’ isOwner and getOwnerWhere methods.
As I am aware you cannot override SugarBeans methods in an upgrade safe way (please correct me if I am wrong) so you need to add the following lines to Account class in “\modules\Accounts\Account.php” file.
PHP Code:/**
* SugarBean isOwner method overridden in a non-upgrade safe way
*/
function isOwner($user_id)
{
//if we don't have an id we must be the owner as we are creating it
if(!isset($this->id))
{
return true;
}
// Fetch currently logged in user
$user = new User();
$user->retrieve($user_id);
$fetched_row = $user->fetched_row;
// Get owner according to user type (users.custom_user_type)
switch ($fetched_row['custom_user_type']) {
case 'sales_rep':
// If user with type "sales_rep" is logged in base ownership on accounts.sales_rep_id field.
if(isset($this->sales_rep_id))
{
if($this->sales_rep_id == $user_id) return true;
return false;
}
break;
case 'merchandiser':
// If user with type "merchandiser" is logged in base ownership on accounts.merchandiser_id field.
if(isset($this->merchandiser_id))
{
if($this->merchandiser_id == $user_id) return true;
return false;
}
break;
// If neither "merchandiser" nor "sales_rep" is logged in then owner is always "sales_rep".
default:
if(isset($this->sales_rep_id))
{
if($this->sales_rep_id == $user_id) return true;
return false;
}
}
return false;
}
/**
* SugarBean getOwnerWhere method overridden in a non-upgrade safe way
*/
function getOwnerWhere($user_id) {
// Fetch currently logged in user
$user = new User();
$user->retrieve($user_id);
$fetched_row = $user->fetched_row;
// Get owner according to user type (users.custom_user_type)
switch ($fetched_row['custom_user_type']) {
case 'sales_rep':
// If user with type "sales_rep" is logged in base ownership on accounts.sales_rep_id field.
if(isset($this->field_defs['sales_rep_id']))
{
return " $this->table_name.sales_rep_id ='$user_id' ";
}
break;
case 'merchandiser':
// If user with type "merchandiser" is logged in base ownership on accounts.merchandiser_id field.
if(isset($this->field_defs['merchandiser_id']))
{
return " $this->table_name.merchandiser_id ='$user_id' ";
}
break;
// If neither "merchandiser" nor "sales_rep" is logged in then owner is always "sales_rep".
default:
if(isset($this->field_defs['sales_rep_id']))
{
return " $this->table_name.sales_rep_id ='$user_id' ";
}
}
}


LinkBack URL
About LinkBacks



Reply With Quote
Bookmarks