Skip to content

Commit

Permalink
Added custom security context to Student Gradebook Job
Browse files Browse the repository at this point in the history
  • Loading branch information
David Wheeler committed Oct 5, 2018
1 parent 77a0204 commit 0b85368
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Sugarcrm\Sugarcrm\custom\gradebook_fake\RecordManager;
use Sugarcrm\Sugarcrm\custom\Security\Subject\StudentsGradebook as GradebookSubject;

class StudentGradebookJob implements RunnableSchedulerJob
{
Expand All @@ -25,26 +26,33 @@ public function setJob(SchedulersJob $job)
public function run($data)
{
if (!empty($data)) {
//Set the security context to
$context = Container::getInstance()->get(Context::class);
$subject = new GradebookSubject([
'contact' => $data
]);
$context->activateSubject($subject);

$bean = $this->getContactBean($data);

$result = false;
try {
//Call the external GradebookFake app to create a new record in it
$rm = $this->getRecordManager();
$success = $rm->createStudentRecord($bean->emailAddress->getPrimaryAddress($bean), $bean->first_name,
$result = $rm->createStudentRecord($bean->emailAddress->getPrimaryAddress($bean), $bean->first_name,
$bean->last_name);
if ($success) {
if ($result) {
$this->job->succeedJob();
return true;
$result = true;
} else {
$this->job->failJob("Record not successfully created in GradebookFake");
return false;
}
} catch (Exception $e) {
$this->job->failJob($e->getMessage());

return false;
}

$context->deactivateSubject($subject);
return $result;
}

$this->job->failJob("Job had no data");
Expand Down
3 changes: 3 additions & 0 deletions package/src/custom/modules/Contacts/Students_Gradebook.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php


use Sugarcrm\Sugarcrm\custom\Security\Subject\StudentsGradebook as GradebookSubject;

/**
* Class Students_Gradebook
* Handles creating a job for the Sugar Job Queue that adds a new student to the GradebookFake app
Expand Down
48 changes: 48 additions & 0 deletions package/src/custom/src/Security/Subject/StudentsGradebook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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;

/**
* A logic hook making changes
*/
final class StudentsGradebook implements Subject
{
/**
* @var string
*/
private $contactId;

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

/**
* {@inheritDoc}
*/
public function jsonSerialize()
{
return [
'_type' => 'students-gradebook',
'contactId' => $this->contactId
];
}
}

0 comments on commit 0b85368

Please sign in to comment.