Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 74
SingleSignonService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
650.00
0.00% covered (danger)
0.00%
0 / 74
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 6
 getUserDetailsForAdminLogin
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 getUserDetailsForStaffLogin
0.00% covered (danger)
0.00%
0 / 1
110.00
0.00% covered (danger)
0.00%
0 / 30
 getUserDetailsForStudentLogin
0.00% covered (danger)
0.00%
0 / 1
90.00
0.00% covered (danger)
0.00%
0 / 27
<?php 
namespace com\linways\core\ams\professional\service;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\nucleus\core\constants\SingleSignonConstant;
/**
 * service for handling signing in from a different service to AMS.
 */
class SingleSignonService extends BaseService
{
    // /Condition 1 - Presence of a static member variable
    private static $_instance = null;
    // private $mapper = [];
    // /Condition 2 - Locked down the constructor
    private function __construct() {
    }
    // Prevent any oustide instantiation of this class
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone() {
    }
    // Prevent any copy of this object
    // /Condition 4 - Have a single globally accessible static method
    public static function getInstance() {
        if (!is_object(self::$_instance))// or if( is_null(self::$_instance) ) or if( self::$_instance == null )
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    public function getUserDetailsForAdminLogin($email){
        $response = NULL;
        $sql = "SELECT adminID, adminAccount, adminPassword, adminName, adminEmail, admintypeID FROM adminaccount WHERE adminEmail = '$email'";
        try {
             $response = $this->executeQueryForObject($sql);
         } catch (\Exception $e) {
             throw new ProfessionalException($e->getCode(), $e->getMessage());
         } 
         return $response;
    }
    /**
     * returns id, username and hashed password of the staff with $fieldName=$fieldValue.
     * Used for single signon functionality.
     * @param $fieldName
     * @param $fieldValue
     * @return mixed
     * @throws ProfessionalException
     */
    public function getUserDetailsForStaffLogin($fieldName, $fieldValue){
        $response = NULL;
        $dbFieldName = NULL;
        switch ($fieldName){
            case SingleSignonConstant::STAFF_ID:
                $dbFieldName ='staffID'; break;
            case SingleSignonConstant::STAFF_LOGIN:
                $dbFieldName ='staffAccount'; break;
            case SingleSignonConstant::STAFF_EMAIL:
                $dbFieldName ='staffEmail'; break;
            default:
                throw new ProfessionalException(ProfessionalException::INVALID_LID_VALUE, "INVALID LID VALUE" );
        }
        $sql = "SELECT staffID as id, staffAccount as username, staffPassword as hashedPassword, staffLock, isResigned FROM staffaccounts WHERE $dbFieldName='$fieldValue'";
        try {
            $response = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        if(count($response)> 1)
            throw new ProfessionalException(ProfessionalException::MULTIPLE_MATCHES, "Multiple Matches found. Please contact institution admin");
        if(count($response) === 1 && $response[0]->id){
            //This is to check that the selected user is blocked or not 
            if($response[0]->staffLock){
                throw new ProfessionalException(ProfessionalException::BLOCKED_USER, "You are currently blocked by administrator");
            }
            //This is to check that the selected user is resigned or not 
            if($response[0]->isResigned){
                throw new ProfessionalException(ProfessionalException::RESIGNED_USER, "You are not allowed to login!");
            }
        }
        return $response[0];
    }
    /**
     * returns id, username and hashed password of the student with $fieldName=$fieldValue.
     * Used for single signon functionality.
     * @param $fieldName
     * @param $fieldValue
     * @return mixed
     * @throws ProfessionalException
     */
    public function getUserDetailsForStudentLogin($fieldName, $fieldValue){
        $response = NULL;
        $dbFieldName = NULL;
        switch ($fieldName){
            case SingleSignonConstant::STUDENT_ID:
                $dbFieldName ='studentID'; break;
            case SingleSignonConstant::STUDENT_LOGIN:
                $dbFieldName ='studentAccount'; break;
            case SingleSignonConstant::STUDENT_EMAIL:
                $dbFieldName ='studentEmail'; break;
            default:
                throw new ProfessionalException(ProfessionalException::INVALID_LID_VALUE, "INVALID LID VALUE" );
        }
        $sql = "SELECT studentID as id, studentAccount as username, studentPassword as hashedPassword,student_lock FROM studentaccount WHERE $dbFieldName='$fieldValue'";
        try {
            $response = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        //This is to prevent multiple matches
        if(count($response)> 1)
            throw new ProfessionalException(ProfessionalException::MULTIPLE_MATCHES, "Multiple Matches found. Please contact institution admin");
        if(count($response) === 1 && $response[0]->id){
            //This is to check that the selected user is blocked or not 
            if($response[0]->student_lock){
                throw new ProfessionalException(ProfessionalException::BLOCKED_USER, "You are currently blocked by administrator");
            }
        }
        return $response[0];
    }
}