Refactoring
This commit is contained in:
125
src/Users/Admin/Api/UsersActionManager.php
Normal file
125
src/Users/Admin/Api/UsersActionManager.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/*
|
||||
This file is part of iCE Hrm.
|
||||
|
||||
iCE Hrm is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
iCE Hrm is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with iCE Hrm. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
------------------------------------------------------------------
|
||||
|
||||
Original work Copyright (c) 2012 [Gamonoid Media Pvt. Ltd]
|
||||
Developer: Thilina Hasantha (thilina.hasantha[at]gmail.com / facebook.com/thilinah)
|
||||
*/
|
||||
namespace Users\Admin\Api;
|
||||
|
||||
use Users\Common\Model\User;
|
||||
use Classes\IceResponse;
|
||||
use Classes\SubActionManager;
|
||||
use Utils\LogManager;
|
||||
|
||||
class UsersActionManager extends SubActionManager
|
||||
{
|
||||
public function changePassword($req)
|
||||
{
|
||||
if (defined('DEMO_MODE')) {
|
||||
return new IceResponse(
|
||||
IceResponse::ERROR,
|
||||
"You are not allowed to change the password in demo mode"
|
||||
);
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$user->Load("id = ?", array($req->id));
|
||||
if ($this->user->user_level == 'Admin' || $this->user->id == $user->id) {
|
||||
if (empty($user->id)) {
|
||||
return new IceResponse(
|
||||
IceResponse::ERROR,
|
||||
"Please save the user first"
|
||||
);
|
||||
}
|
||||
$user->password = md5($req->pwd);
|
||||
$ok = $user->Save();
|
||||
if (!$ok) {
|
||||
return new IceResponse(IceResponse::ERROR, $user->ErrorMsg());
|
||||
}
|
||||
return new IceResponse(IceResponse::SUCCESS, $user);
|
||||
}
|
||||
return new IceResponse(IceResponse::ERROR);
|
||||
}
|
||||
|
||||
public function saveUser($req)
|
||||
{
|
||||
if ($this->user->user_level == 'Admin') {
|
||||
$user = new User();
|
||||
$user->Load("email = ?", array($req->email));
|
||||
|
||||
if ($user->email == $req->email) {
|
||||
return new IceResponse(
|
||||
IceResponse::ERROR,
|
||||
"User with same email already exists"
|
||||
);
|
||||
}
|
||||
|
||||
$user->Load("username = ?", array($req->username));
|
||||
|
||||
if ($user->username == $req->username) {
|
||||
return new IceResponse(
|
||||
IceResponse::ERROR,
|
||||
"User with same username already exists"
|
||||
);
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$user->email = $req->email;
|
||||
$user->username = $req->username;
|
||||
$password = $this->generateRandomString(6);
|
||||
$user->password = md5($password);
|
||||
$user->employee = (empty($req->employee) || $req->employee == "NULL" )?null:$req->employee;
|
||||
$user->user_level = $req->user_level;
|
||||
$user->last_login = date("Y-m-d H:i:s");
|
||||
$user->last_update = date("Y-m-d H:i:s");
|
||||
$user->created = date("Y-m-d H:i:s");
|
||||
|
||||
$employee = null;
|
||||
if (!empty($user->employee)) {
|
||||
$employee = $this->baseService->getElement('Employee', $user->employee, null, true);
|
||||
}
|
||||
|
||||
$ok = $user->Save();
|
||||
if (!$ok) {
|
||||
LogManager::getInstance()->info($user->ErrorMsg()."|".json_encode($user));
|
||||
return new IceResponse(IceResponse::ERROR, "Error occured while saving the user");
|
||||
}
|
||||
$user->password = "";
|
||||
$user = $this->baseService->cleanUpAdoDB($user);
|
||||
|
||||
if (!empty($this->emailSender)) {
|
||||
$usersEmailSender = new UsersEmailSender($this->emailSender, $this);
|
||||
$usersEmailSender->sendWelcomeUserEmail($user, $password, $employee);
|
||||
}
|
||||
return new IceResponse(IceResponse::SUCCESS, $user);
|
||||
}
|
||||
return new IceResponse(IceResponse::ERROR, "Not Allowed");
|
||||
}
|
||||
|
||||
private function generateRandomString($length = 10)
|
||||
{
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
return $randomString;
|
||||
}
|
||||
}
|
||||
36
src/Users/Admin/Api/UsersAdminManager.php
Normal file
36
src/Users/Admin/Api/UsersAdminManager.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace Users\Admin\Api;
|
||||
|
||||
use Classes\AbstractModuleManager;
|
||||
use Users\Common\Model\User;
|
||||
|
||||
class UsersAdminManager extends AbstractModuleManager
|
||||
{
|
||||
|
||||
public function initializeUserClasses()
|
||||
{
|
||||
}
|
||||
|
||||
public function initializeFieldMappings()
|
||||
{
|
||||
}
|
||||
|
||||
public function initializeDatabaseErrorMappings()
|
||||
{
|
||||
}
|
||||
|
||||
public function setupModuleClassDefinitions()
|
||||
{
|
||||
|
||||
$this->addModelClass('User');
|
||||
$this->addModelClass('UserRole');
|
||||
}
|
||||
|
||||
public function getDashboardItemData()
|
||||
{
|
||||
$data = array();
|
||||
$user = new User();
|
||||
$data['numberOfUsers'] = $user->Count("1 = 1");
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
51
src/Users/Admin/Api/UsersEmailSender.php
Normal file
51
src/Users/Admin/Api/UsersEmailSender.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace Users\Admin\Api;
|
||||
|
||||
use Classes\Email\EmailSender;
|
||||
use Users\Common\Model\User;
|
||||
use Classes\SubActionManager;
|
||||
use Utils\LogManager;
|
||||
|
||||
class UsersEmailSender
|
||||
{
|
||||
|
||||
var $emailSender = null;
|
||||
var $subActionManager = null;
|
||||
|
||||
public function __construct(EmailSender $emailSender, SubActionManager $subActionManager)
|
||||
{
|
||||
$this->emailSender = $emailSender;
|
||||
$this->subActionManager = $subActionManager;
|
||||
}
|
||||
|
||||
public function sendWelcomeUserEmail(User $user, string $password, $employee = null)
|
||||
{
|
||||
|
||||
$params = array();
|
||||
if (!empty($employee)) {
|
||||
$params['name'] = $employee->first_name." ".$employee->last_name;
|
||||
} else {
|
||||
$params['name'] = $user->username;
|
||||
}
|
||||
$params['url'] = CLIENT_BASE_URL;
|
||||
$params['password'] = $password;
|
||||
$params['email'] = $user->email;
|
||||
$params['username'] = $user->username;
|
||||
|
||||
$email = $this->subActionManager->getEmailTemplate('welcomeUser.html');
|
||||
|
||||
$emailTo = null;
|
||||
if (!empty($user)) {
|
||||
$emailTo = $user->email;
|
||||
}
|
||||
|
||||
if (!empty($emailTo)) {
|
||||
if (!empty($this->emailSender)) {
|
||||
LogManager::getInstance()->info("[sendWelcomeUserEmail] sending email to $emailTo : ".$email);
|
||||
$this->emailSender->sendEmail("Your IceHrm account is ready", $emailTo, $email, $params);
|
||||
}
|
||||
} else {
|
||||
LogManager::getInstance()->info("[sendWelcomeUserEmail] email is empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
84
src/Users/Common/Model/User.php
Normal file
84
src/Users/Common/Model/User.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/*
|
||||
This file is part of iCE Hrm.
|
||||
|
||||
iCE Hrm is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
iCE Hrm is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with iCE Hrm. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
------------------------------------------------------------------
|
||||
|
||||
Original work Copyright (c) 2012 [Gamonoid Media Pvt. Ltd]
|
||||
Developer: Thilina Hasantha (thilina.hasantha[at]gmail.com / facebook.com/thilinah)
|
||||
*/
|
||||
namespace Users\Common\Model;
|
||||
|
||||
use Classes\BaseService;
|
||||
use Model\BaseModel;
|
||||
use Classes\IceResponse;
|
||||
|
||||
class User extends BaseModel
|
||||
{
|
||||
public function getAdminAccess()
|
||||
{
|
||||
return array("get","element","save","delete");
|
||||
}
|
||||
|
||||
public function getUserAccess()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function validateSave($obj)
|
||||
{
|
||||
$userTemp = new User();
|
||||
|
||||
if (empty($obj->id)) {
|
||||
$users = $userTemp->Find("email = ?", array($obj->email));
|
||||
if (count($users) > 0) {
|
||||
return new IceResponse(IceResponse::ERROR, "A user with same authentication email already exist");
|
||||
}
|
||||
} else {
|
||||
$users = $userTemp->Find("email = ? and id <> ?", array($obj->email, $obj->id));
|
||||
if (count($users) > 0) {
|
||||
return new IceResponse(IceResponse::ERROR, "A user with same authentication email already exist");
|
||||
}
|
||||
|
||||
//Check if you are trying to change user level
|
||||
$oldUser = new User();
|
||||
$oldUser->Load("id = ?", array($obj->id));
|
||||
if ($oldUser->user_level != $obj->user_level && $oldUser->user_level == 'Admin') {
|
||||
$adminUsers = $userTemp->Find("user_level = ?", array("Admin"));
|
||||
if (count($adminUsers) == 1 && $adminUsers[0]->id == $obj->id) {
|
||||
return new IceResponse(IceResponse::ERROR, "You are the only admin user for the application. You are not allowed to revoke your admin rights");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Check if the user have rights to the default module
|
||||
if (!empty($obj->default_module)) {
|
||||
$module = new \Module();
|
||||
$module->Load("id = ?", array($obj->default_module));
|
||||
if ($module->mod_group == "user") {
|
||||
$module->mod_group = "modules";
|
||||
}
|
||||
$moduleManager = BaseService::getInstance()->getModuleManager($module->mod_group, $module->name);
|
||||
if (!BaseService::getInstance()->isModuleAllowedForGivenUser($moduleManager, $obj)) {
|
||||
return new IceResponse(IceResponse::ERROR, "This module can not be set as the default module for the user since the user do not have access to this module");
|
||||
}
|
||||
}
|
||||
|
||||
return new IceResponse(IceResponse::SUCCESS, "");
|
||||
}
|
||||
|
||||
var $_table = 'Users';
|
||||
}
|
||||
41
src/Users/Common/Model/UserRole.php
Normal file
41
src/Users/Common/Model/UserRole.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/*
|
||||
This file is part of iCE Hrm.
|
||||
|
||||
iCE Hrm is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
iCE Hrm is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with iCE Hrm. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
------------------------------------------------------------------
|
||||
|
||||
Original work Copyright (c) 2012 [Gamonoid Media Pvt. Ltd]
|
||||
Developer: Thilina Hasantha (thilina.hasantha[at]gmail.com / facebook.com/thilinah)
|
||||
*/
|
||||
|
||||
namespace Users\Common\Model;
|
||||
|
||||
use Model\BaseModel;
|
||||
|
||||
class UserRole extends BaseModel
|
||||
{
|
||||
public function getAdminAccess()
|
||||
{
|
||||
return array('get','element','save','delete');
|
||||
}
|
||||
|
||||
public function getUserAccess()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
var $_table = 'UserRoles';
|
||||
}
|
||||
Reference in New Issue
Block a user