Skip to content

Commit c091a4b

Browse files
authored
Merge pull request #205 from sumitwebkul/gli-483
Prevent deleting profile which is used by at least one employee
2 parents a25db22 + 6b18407 commit c091a4b

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

classes/Profile.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,17 @@ public function add($autodate = true, $null_values = false)
9494

9595
public function delete()
9696
{
97-
if (parent::delete()) {
98-
return (
99-
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'access` WHERE `id_profile` = '.(int)$this->id)
100-
&& Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'module_access` WHERE `id_profile` = '.(int)$this->id)
101-
);
97+
// check if any employee exists of this deleting profile before delete
98+
$profileEmployees = Employee::getEmployeesByProfile($this->id);
99+
if (empty($profileEmployees)) {
100+
if (parent::delete()) {
101+
return (
102+
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'access` WHERE `id_profile` = '.(int)$this->id)
103+
&& Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'module_access` WHERE `id_profile` = '.(int)$this->id)
104+
);
105+
}
102106
}
107+
103108
return false;
104109
}
105110

controllers/admin/AdminProfilesController.php

+36
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,40 @@ public function initPageHeaderToolbar()
115115

116116
parent::initPageHeaderToolbar();
117117
}
118+
119+
public function processDelete()
120+
{
121+
if (Validate::isLoadedObject($object = $this->loadObject())) {
122+
// check if any employee exists of this deleting profile before delete
123+
$profileEmployees = Employee::getEmployeesByProfile($object->id);
124+
if (empty($profileEmployees)) {
125+
return parent::processDelete();
126+
} else {
127+
$this->errors[] = $this->l('Failed to delete profile, because it is assigned to at least one employee.');
128+
}
129+
} else {
130+
$this->errors[] = $this->l('An error occurred while deleting the profile.').' '.$this->l('(cannot load object)');
131+
}
132+
133+
return false;
134+
}
135+
136+
protected function processBulkDelete()
137+
{
138+
if (is_array($this->boxes) && !empty($this->boxes)) {
139+
foreach ($this->boxes as $idProfile) {
140+
// check if any employee exists of this deleting profile before delete
141+
$profileEmployees = Employee::getEmployeesByProfile($idProfile);
142+
if (!empty($profileEmployees)) {
143+
$this->errors[] = $this->l('Failed to delete profile with id').' - #'.$idProfile.' '.$this->l('because it is assigned to at least one employee. Please remove it from selection.');
144+
}
145+
}
146+
}
147+
148+
if (empty($this->errors)) {
149+
return parent::processBulkDelete();
150+
}
151+
152+
return false;
153+
}
118154
}

0 commit comments

Comments
 (0)