Skip to content

Commit

Permalink
Added custom security context to Applicant Programming Score hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Wheeler committed Oct 8, 2018
1 parent 77a0204 commit 8520d10
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
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);
$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
];
}
}

0 comments on commit 8520d10

Please sign in to comment.