Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 115 |
UserService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
306.00 | |
0.00% |
0 / 115 |
__construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
__clone | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
getInstance | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
getUser | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 27 |
|||
getAllUserRoleCodes | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 30 |
|||
getUserModules | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 48 |
<?php | |
namespace com\linways\core\ams\professional\service; | |
use com\linways\core\ams\professional\constant\UserType; | |
use com\linways\core\ams\professional\dto\ProductConfiguration\User; | |
use com\linways\core\ams\professional\exception\ProfessionalException; | |
use com\linways\core\ams\professional\mapper\UserServiceMapper; | |
use com\linways\core\ams\professional\service\productConfiguration\UserMenuService; | |
/** | |
* | |
* @Date 05/08/20 | |
* @author JithinVijayan <jithin@linways.com> | |
*/ | |
class UserService extends BaseService | |
{ | |
/** | |
* @var null | |
*/ | |
private static $_instance = null; | |
/** | |
* @var array | |
*/ | |
private $mapper = []; | |
/** | |
* UserMenuService constructor. | |
*/ | |
private function __construct() | |
{ | |
$this->mapper = UserServiceMapper::getInstance()->getMapper(); | |
} | |
/** | |
* Preventing outside cloning | |
*/ | |
private function __clone() | |
{ | |
} | |
/** | |
* @return UserService|null | |
*/ | |
public static function getInstance() | |
{ | |
if (!is_object(self::$_instance)) | |
self::$_instance = new self(); | |
return self::$_instance; | |
} | |
/** | |
* Returns student details. | |
* Note: Here staff account is used as staff code because we need a unique key as staff code. In some colleges, | |
* they using same staff code for different staff accounts. | |
* | |
* @param $userId | |
* @param $userType | |
* @return Object|User | |
* @throws ProfessionalException | |
*/ | |
public function getUser($userId, $userType) | |
{ | |
switch ($userType) { | |
case UserType::STAFF: | |
$sql = "SELECT sa.staffID as id, sa.staffAccount as code, sa.staffEmail as primaryEmail, '$userType' as user_type,sa.staffName as name, | |
r.id as role_id,r.name as role_name,r.code as role_code | |
FROM staffaccounts sa | |
LEFT JOIN user_account_roles uar ON uar.user_type ='$userType' AND uar.user_id = sa.staffID | |
LEFT JOIN roles r on uar.role_id = r.id | |
WHERE sa.staffID = $userId"; | |
break; | |
case UserType::STUDENT: | |
$sql = "SELECT sa.studentID as id, sa.studentAccount as code, '$userType' as user_type,sa.studentName as name, | |
r.id as role_id,r.name as role_name,r.code as role_code | |
FROM studentaccount sa | |
LEFT JOIN user_account_roles uar ON uar.user_type ='$userType' AND uar.user_id = sa.studentID | |
LEFT JOIN roles r on uar.role_id = r.id | |
WHERE studentID = $userId"; | |
break; | |
default: | |
throw new ProfessionalException(ProfessionalException::INVALID_USER_TYPE, "Invalid user type given"); | |
} | |
try { | |
return $this->executeQueryForObject($sql, false, $this->mapper[UserServiceMapper::USER_MAPPER]); | |
} catch (\Exception $e) { | |
throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* @param $userId | |
* @param $userType | |
* @return array | |
* @throws ProfessionalException | |
*/ | |
public function getAllUserRoleCodes($userId, $userType) | |
{ | |
switch ($userType) { | |
case UserType::STAFF: | |
$sql = "SELECT DISTINCT r.code as role_code | |
FROM staffaccounts sa | |
LEFT JOIN user_account_roles uar ON uar.user_type ='$userType' AND uar.user_id = sa.staffID | |
LEFT JOIN roles r on uar.role_id = r.id | |
WHERE sa.staffID = $userId"; | |
break; | |
case UserType::STUDENT: | |
$sql = "SELECT DISTINCT r.code as role_code | |
FROM studentaccount sa | |
LEFT JOIN user_account_roles uar ON uar.user_type ='$userType' AND uar.user_id = sa.studentID | |
LEFT JOIN roles r on uar.role_id = r.id | |
WHERE studentID = $userId"; | |
break; | |
default: | |
throw new ProfessionalException(ProfessionalException::INVALID_USER_TYPE, "Invalid user type given"); | |
} | |
try { | |
$resultRoleCodes = $this->executeQueryForList($sql); | |
$roleCodes = []; | |
foreach ($resultRoleCodes as $roleCode) { | |
$roleCodes[] = $roleCode->role_code; | |
} | |
return $roleCodes; | |
} catch (\Exception $e) { | |
throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* Get user's roles based on user Id | |
* @param stdClass $userDetails | |
* @return Array $modules | |
* @throws ProfessionalException | |
*/ | |
public function getUserModules ( $userDetails ) { | |
$modules = []; | |
try { | |
if (empty($userDetails->userId) || empty($userDetails->userType)) { | |
throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS, "Invalid user id or user type"); | |
} | |
$sql = "SELECT DISTINCT | |
ap.module | |
FROM | |
auth_permission ap | |
INNER JOIN | |
auth_role_permissions arp ON ap.id = arp.auth_permission_id | |
INNER JOIN | |
user_account_roles uar ON uar.role_id = arp.role_id | |
WHERE | |
uar.user_id = :userId | |
AND uar.user_type = :userType | |
UNION | |
SELECT DISTINCT | |
ap.module | |
FROM | |
auth_permission ap | |
INNER JOIN | |
auth_group_permission agp ON ap.id = agp.auth_permission_id | |
INNER JOIN | |
auth_staffaccounts_group asg ON asg.auth_group_id = agp.auth_group_id | |
WHERE | |
asg.staff_id = :userId | |
UNION | |
SELECT DISTINCT | |
ap.module | |
FROM | |
auth_permission ap | |
INNER JOIN | |
auth_staffaccounts_permission asp ON ap.id = asp.auth_permission_id | |
WHERE | |
asp.staff_id = :userId "; | |
$params = [ | |
'userId' => $userDetails->userId, | |
'userType' => $userDetails->userType | |
]; | |
$modules = $this->executeQueryForListUsingPreparedStatement($sql, $params); | |
$modules = array_column($modules, 'module'); | |
} | |
catch (\Exception $e) { | |
throw new ProfessionalException($e->getCode(),$e->getMessage()); | |
} | |
return $modules; | |
} | |
} |