Skip to content

Commit 01d6f4d

Browse files
author
David Wheeler
committed
Added custom security context to Applicant Programming Score hook.
1 parent 77a0204 commit 01d6f4d

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

package/pack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@
400400
'default_value' => NULL,
401401
'date_modified' => '2018-08-06 20:33:40',
402402
'deleted' => '0',
403-
'audited' => '0',
403+
'audited' => '1',
404404
'mass_update' => '0',
405405
'duplicate_merge' => '1',
406406
'reportable' => '1',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<?php
22

33
$app_strings['ERROR_GPA_NOT_IN_RANGE'] = 'GPA must be at least 0 and no higher than 4.0.';
4+
$app_strings['LBL_AUDIT_SUBJECT_APS-HOOK'] = 'Applicant Programming Score Logic';
5+

package/src/custom/Extension/modules/Leads/Ext/Language/en_us.ProfessorMStudioChanges.lang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
$mod_strings['LBL_ALIAS'] = 'Alias';
3030
$mod_strings['LBL_DESCRIPTION'] = 'Origin Story';
3131
$mod_strings['LBL_PROGRAMMINGLANGUAGES'] = 'Programming Languages';
32+
$mod_strings['LBL_PROGRAMMING_SCORE_C'] = 'Programming Score';
3233
$mod_strings['LBL_TRANSCRIPT'] = 'Transcript';
3334
$mod_strings['LBL_HIGHSCHOOL'] = 'High School';
3435
$mod_strings['LBL_GPA'] = 'Grade Point Average (GPA)';

package/src/custom/modules/Contacts/Students_Gradebook.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
4+
use Sugarcrm\Sugarcrm\custom\Security\Subject\StudentsGradebook as GradebookSubject;
5+
36
/**
47
* Class Students_Gradebook
58
* Handles creating a job for the Sugar Job Queue that adds a new student to the GradebookFake app

package/src/custom/modules/Leads/ApplicantProgrammingScore.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<?php
2+
use Sugarcrm\Sugarcrm\DependencyInjection\Container;
3+
use Sugarcrm\Sugarcrm\Security\Context;
4+
5+
use Sugarcrm\Sugarcrm\custom\Security\Subject\ApplicantProgrammingScore as ApsSubject;
6+
27

38
/**
49
* Class ApplicantProgrammingScore
510
* Updates an applicant's Programming Score
611
*/
712
class ApplicantProgrammingScore
813
{
14+
private $version = '1.0.0';
915
/**
1016
* Update an applicant's Programming Score and then update calculated fields.
1117
*
@@ -15,6 +21,12 @@ class ApplicantProgrammingScore
1521
*/
1622
public function updateProgrammingScore($bean, $event, $arguments)
1723
{
24+
//Set the security context to better track the source of changes
25+
$context = Container::getInstance()->get(Context::class);
26+
$subject = new ApsSubject($bean->id);
27+
$context->activateSubject($subject);
28+
$context->setAttribute('aps_calc_version', $this->version);
29+
1830
// The programming languages are stored as a comma separated list in the bean. Convert them to an array.
1931
$programmingLanguages = explode(",", $bean->programminglanguages_c);
2032

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

2436
// Update the calculated fields. This is necessary for the Rating Star field to be updated immediately.
2537
$bean->updateCalculatedFields();
26-
38+
39+
//Have to commit the changes we made to the audit log because we didn't call save.
40+
//Passing the subject rather than null here would override the additional attributes that were set on the context
41+
$bean->commitAuditedStateChanges(null);
42+
//Always deactivate the subject before leaving the function
43+
$context->deactivateSubject($subject);
2744
}
2845

2946
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/*
3+
* Your installation or use of this SugarCRM file is subject to the applicable
4+
* terms available at
5+
* http://support.sugarcrm.com/Resources/Master_Subscription_Agreements/.
6+
* If you do not agree to all of the applicable terms or do not have the
7+
* authority to bind the entity as an authorized representative, then do not
8+
* install or use this SugarCRM file.
9+
*
10+
* Copyright (C) SugarCRM Inc. All rights reserved.
11+
*/
12+
13+
namespace Sugarcrm\Sugarcrm\custom\Security\Subject;
14+
15+
use Sugarcrm\Sugarcrm\Security\Subject;
16+
17+
/**
18+
* A logic hook making changes
19+
*/
20+
final class ApplicantProgrammingScore implements Subject
21+
{
22+
/**
23+
* @var string
24+
*/
25+
private $applicantId;
26+
27+
/**
28+
* Constructor
29+
*
30+
* @param string $class
31+
* @param string $method
32+
*/
33+
public function __construct($applicantId)
34+
{
35+
$this->applicantId = $applicantId;
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public function jsonSerialize()
42+
{
43+
return [
44+
'_type' => 'aps-hook',
45+
'applicant_id' => $this->applicantId
46+
];
47+
}
48+
}

0 commit comments

Comments
 (0)