Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom security context to APS calculation hook #111

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package/pack.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
'default_value' => NULL,
'date_modified' => '2018-08-06 20:33:40',
'deleted' => '0',
'audited' => '0',
'audited' => '1',
'mass_update' => '0',
'duplicate_merge' => '1',
'reportable' => '1',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$app_strings['LBL_AUDIT_SUBJECT_APS-HOOK'] = 'Applicant Programming Score Logic';
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
$mod_strings['LBL_ALIAS'] = 'Alias';
$mod_strings['LBL_DESCRIPTION'] = 'Origin Story';
$mod_strings['LBL_PROGRAMMINGLANGUAGES'] = 'Programming Languages';
$mod_strings['LBL_PROGRAMMING_SCORE_C'] = 'Programming Score';
$mod_strings['LBL_TRANSCRIPT'] = 'Transcript';
$mod_strings['LBL_HIGHSCHOOL'] = 'High School';
$mod_strings['LBL_GPA'] = 'Grade Point Average (GPA)';
Expand Down
19 changes: 18 additions & 1 deletion package/src/custom/modules/Leads/ApplicantProgrammingScore.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<?php
use Sugarcrm\Sugarcrm\DependencyInjection\Container;
use Sugarcrm\Sugarcrm\Security\Context;

use Sugarcrm\Sugarcrm\custom\Security\Subject\ApplicantProgrammingScore as ApsSubject;


/**
* Class ApplicantProgrammingScore
* Updates an applicant's Programming Score
*/
class ApplicantProgrammingScore
{
private $version = '1.0.0';
/**
* Update an applicant's Programming Score and then update calculated fields.
*
Expand All @@ -15,6 +21,12 @@ class ApplicantProgrammingScore
*/
public function updateProgrammingScore($bean, $event, $arguments)
{
//Set the security context to better track the source of changes
$context = Container::getInstance()->get(Context::class);
$subject = new ApsSubject($bean->id);
$context->activateSubject($subject);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sergey had a suggestion of using a try catch finally block to make sure subject is deactivated when exceptions are thrown.

$context->setAttribute('aps_calc_version', $this->version);

// The programming languages are stored as a comma separated list in the bean. Convert them to an array.
$programmingLanguages = explode(",", $bean->programminglanguages_c);

Expand All @@ -23,7 +35,12 @@ public function updateProgrammingScore($bean, $event, $arguments)

// Update the calculated fields. This is necessary for the Rating Star field to be updated immediately.
$bean->updateCalculatedFields();


//Have to commit the changes we made to the audit log because we didn't call save.
//Passing the subject rather than null here would override the additional attributes that were set on the context
$bean->commitAuditedStateChanges(null);
//Always deactivate the subject before leaving the function
$context->deactivateSubject($subject);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* Your installation or use of this SugarCRM file is subject to the applicable
* terms available at
* http://support.sugarcrm.com/Resources/Master_Subscription_Agreements/.
* If you do not agree to all of the applicable terms or do not have the
* authority to bind the entity as an authorized representative, then do not
* install or use this SugarCRM file.
*
* Copyright (C) SugarCRM Inc. All rights reserved.
*/

namespace Sugarcrm\Sugarcrm\custom\Security\Subject;

use Sugarcrm\Sugarcrm\Security\Subject;

/**
* The APS hook making changes
*/
final class ApplicantProgrammingScore implements Subject
{
/**
* @var string
*/
private $applicantId;

/**
* Constructor
*
* @param string $applicantId
*/
public function __construct($applicantId)
{
$this->applicantId = $applicantId;
}

/**
* {@inheritDoc}
*/
public function jsonSerialize()
{
return [
'_type' => 'aps-hook',
'applicant_id' => $this->applicantId
];
}
}