Skip to content

Commit 0b85368

Browse files
author
David Wheeler
committed
Added custom security context to Student Gradebook Job
1 parent 77a0204 commit 0b85368

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

package/src/custom/Extension/modules/Schedulers/Ext/ScheduledTasks/StudentGradebookJob.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Sugarcrm\Sugarcrm\custom\gradebook_fake\RecordManager;
4+
use Sugarcrm\Sugarcrm\custom\Security\Subject\StudentsGradebook as GradebookSubject;
45

56
class StudentGradebookJob implements RunnableSchedulerJob
67
{
@@ -25,26 +26,33 @@ public function setJob(SchedulersJob $job)
2526
public function run($data)
2627
{
2728
if (!empty($data)) {
29+
//Set the security context to
30+
$context = Container::getInstance()->get(Context::class);
31+
$subject = new GradebookSubject([
32+
'contact' => $data
33+
]);
34+
$context->activateSubject($subject);
35+
2836
$bean = $this->getContactBean($data);
2937

38+
$result = false;
3039
try {
3140
//Call the external GradebookFake app to create a new record in it
3241
$rm = $this->getRecordManager();
33-
$success = $rm->createStudentRecord($bean->emailAddress->getPrimaryAddress($bean), $bean->first_name,
42+
$result = $rm->createStudentRecord($bean->emailAddress->getPrimaryAddress($bean), $bean->first_name,
3443
$bean->last_name);
35-
if ($success) {
44+
if ($result) {
3645
$this->job->succeedJob();
37-
return true;
46+
$result = true;
3847
} else {
3948
$this->job->failJob("Record not successfully created in GradebookFake");
40-
return false;
4149
}
4250
} catch (Exception $e) {
4351
$this->job->failJob($e->getMessage());
44-
45-
return false;
4652
}
4753

54+
$context->deactivateSubject($subject);
55+
return $result;
4856
}
4957

5058
$this->job->failJob("Job had no data");

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
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 StudentsGradebook implements Subject
21+
{
22+
/**
23+
* @var string
24+
*/
25+
private $contactId;
26+
27+
/**
28+
* Constructor
29+
*
30+
* @param string $class
31+
* @param string $method
32+
*/
33+
public function __construct($contactId)
34+
{
35+
$this->contactId = $contactId;
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public function jsonSerialize()
42+
{
43+
return [
44+
'_type' => 'students-gradebook',
45+
'contactId' => $this->contactId
46+
];
47+
}
48+
}

0 commit comments

Comments
 (0)