Skip to content
Permalink
Browse files

Code Cleanup/Optimization

- add a new method called auditPlugin that returns true/false depending on if the plugin exists and is active
- reduce the amound of info in the $type array. we can get the rest of the info needed in auditObjectEvent
- remove unnecessary model info
- etc
- create flagChanged/statusChanged functions to see if flags have changed
  • Loading branch information...
aydreeihn committed Jul 1, 2019
1 parent 7cfc062 commit a4bd53b46a53e4d7055f36f60da2874771289c2f
@@ -488,10 +488,11 @@ function login($staff, $bk) {
sprintf(_S("%s logged in [%s], via %s"), $staff->getUserName(),
$_SERVER['REMOTE_ADDR'], get_class($bk))); //Debug.
$agent = Staff::lookup($staff->getId());
$type = array('type' => 'login', 'data' => array('person' => $agent->getName()->name));
Signal::send('staff.login', $agent, $type);
if (PluginManager::auditPlugin()) {
$agent = Staff::lookup($staff->getId());
$type = array('type' => 'login');
Signal::send('person.login', $agent, $type);
}
// Tag the authkey.
$authkey = $bk::$id.':'.$authkey;
@@ -532,9 +533,11 @@ static function signOut($staff) {
$staff->getUserName(),
$_SERVER['REMOTE_ADDR'])); //Debug.
$agent = Staff::lookup($staff->getId());
$type = array('type' => 'logout', 'data' => array('person' => $agent->getName()->name));
Signal::send('staff.logout', $agent, $type);
if (PluginManager::auditPlugin()) {
$agent = Staff::lookup($staff->getId());
$type = array('type' => 'logout');
Signal::send('person.logout', $agent, $type);
}
Signal::send('auth.logout', $staff);
}
@@ -684,9 +687,11 @@ function login($user, $bk) {
$user->getUserName(), $user->getId(), $_SERVER['REMOTE_ADDR']);
$ost->logDebug(_S('User login'), $msg);
$u = User::lookup($user->getId());
$type = array('type' => 'login', 'data' => array('person' => $u->getName()->name));
Signal::send('staff.login', $u, $type);
if (PluginManager::auditPlugin()) {
$u = User::lookup($user->getId());
$type = array('type' => 'login');
Signal::send('person.login', $u, $type);
}
if ($bk->supportsInteractiveAuthentication() && ($acct=$user->getAccount()))
$acct->cancelResetTokens();
@@ -724,9 +729,11 @@ static function signOut($user) {
sprintf(_S("%s logged out [%s]" /* Tokens are <username> and <ip> */),
$user->getUserName(), $_SERVER['REMOTE_ADDR']));
$u = User::lookup($user->getId());
$type = array('type' => 'logout', 'data' => array('person' => $u->getName()->name));
Signal::send('staff.logout', $u, $type);
if (PluginManager::auditPlugin()) {
$u = User::lookup($user->getId());
$type = array('type' => 'logout');
Signal::send('person.logout', $u, $type);
}
}
protected function getAuthKey($user) {
@@ -911,10 +918,11 @@ function authstrike($credentials) {
if ($staffId)
$staff = Staff::lookup($staffId[0]);
if ($staff) {
$agent = Staff::lookup($staff->getId());
$type = array('type' => 'login',
'data' => array('msg' => sprintf('Excessive login attempts (%s)', $authsession['strikes']), 'person' => $agent->getName()->name));
Signal::send('staff.login', $agent, $type);
if (PluginManager::auditPlugin()) {
$agent = Staff::lookup($staff->getId());
$type = array('type' => 'login', 'msg' => sprintf('Excessive login attempts (%s)', $authsession['strikes']));
Signal::send('person.login', $agent, $type);
}
}
}
@@ -988,10 +996,11 @@ function authstrike($credentials) {
$user = User::lookup($id);
if ($user) {
$u = User::lookup($user->getId());
$type = array('type' => 'login',
'data' => array('msg' => sprintf('Excessive login attempts (%s)', $authsession['strikes']), 'person' => $u->getName()->name));
Signal::send('user.login', $u, $type);
if (PluginManager::auditPlugin()) {
$u = User::lookup($user->getId());
$type = array('type' => 'login', 'msg' => sprintf('Excessive login attempts (%s)', $authsession['strikes']));
Signal::send('person.login', $u, $type);
}
}
}
@@ -110,8 +110,6 @@ function create($key, $value) {
}
function update($key, $value) {
global $thisstaff;
if (!$key)
return false;
elseif (!isset($this->config[$key]))
@@ -121,9 +119,9 @@ function update($key, $value) {
$before = $item->value;
$item->value = $value;
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
if (PluginManager::auditPlugin()) {
if ($before != $item->value) {
$type = array('type' => 'edited', 'data' => array('person' => $thisstaff->getName()->name, 'key' => $item->ht['key']));
$type = array('type' => 'edited', 'key' => $item->ht['key']);
Signal::send('object.edited', $item, $type);
}
}
@@ -614,6 +614,12 @@ function hasFlag($flag) {
return ($this->get('flags', 0) & $flag) != 0;
}
function flagChanged($flag, $var) {
if (($this->hasflag($flag) && !$var) ||
(!$this->hasflag($flag) && $var))
return true;
}
function export($dept, $criteria=null, $filename='') {
include_once(INCLUDE_DIR.'class.error.php');
$members = $dept->getMembers();
@@ -818,20 +824,18 @@ function update($vars, &$errors) {
return false;
$vars['disable_auto_claim'] = isset($vars['disable_auto_claim']) ? 1 : 0;
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
if (PluginManager::auditPlugin()) {
//flags
if (($this->hasflag(self::FLAG_DISABLE_AUTO_CLAIM) && !$vars['disable_auto_claim']) ||
(!$this->hasflag(self::FLAG_DISABLE_AUTO_CLAIM) && $vars['disable_auto_claim']))
$disableAutoClaim = true;
$disableAutoClaim = $this->flagChanged(self::FLAG_DISABLE_AUTO_CLAIM, $vars['disable_auto_claim']);
$ticketAssignment = ($this->getAssignmentFlag() != $vars['assignment_flag']);
foreach ($vars as $key => $value) {
if ($key == 'status' && $this->getStatus() && strtolower($this->getStatus()) != $value) {
$loggedUpdate = true;
$type = array('type' => 'edited', 'data' => array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'type' => ucfirst($value)));
$type = array('type' => 'edited', 'status' => ucfirst($value));
Signal::send('object.edited', $this, $type);
} elseif ((isset($this->$key) && ($this->$key != $value) && $key != 'members') ||
($disableAutoClaim && $key == 'disable_auto_claim')) {
$loggedUpdate = true;
$type = array('type' => 'edited', 'data' => array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => $key));
($disableAutoClaim && $key == 'disable_auto_claim') ||
$ticketAssignment && $key == 'assignment_flag') {
$type = array('type' => 'edited', 'key' => $key);
Signal::send('object.edited', $this, $type);
}
}
@@ -901,9 +905,6 @@ function update($vars, &$errors) {
// The ID wasn't available until after the commit
$this->path = $this->getFullPath();
$this->save();
} elseif (!$loggedUpdate) {
$type = array('type' => 'edited', 'data' => array('name' => $this->getName(), 'person' => $thisstaff->getName()->name));
Signal::send('object.edited', $this, $type);
}
return true;
}
@@ -919,8 +920,6 @@ function update($vars, &$errors) {
}
function updateAccess($access, &$errors) {
global $thisstaff;
reset($access);
$dropped = array();
foreach ($this->extended as $DA)
@@ -947,8 +946,8 @@ function updateAccess($access, &$errors) {
'staff_id' => $staff_id, 'role_id' => $role_id
));
$this->extended->add($da);
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
$type = array('type' => 'edited', 'data' => array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => 'Staff Added'));
if (PluginManager::auditPlugin()) {
$type = array('type' => 'edited', 'key' => 'Staff Added');
Signal::send('object.edited', $this, $type);
}
}
@@ -963,8 +962,8 @@ function updateAccess($access, &$errors) {
return false;
if ($dropped) {
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
$type = array('type' => 'edited', 'data' => array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => 'Staff Removed'));
if (PluginManager::auditPlugin()) {
$type = array('type' => 'edited', 'key' => 'Staff Removed');
Signal::send('object.edited', $this, $type);
}
$this->extended->saveAll();
@@ -389,10 +389,10 @@ function update($vars, &$errors=false) {
//checkboxes
$vars['noautoresp'] = isset($vars['noautoresp']) ? 1 : 0;
$vars['smtp_spoofing'] = isset($vars['smtp_spoofing']) ? 1 : 0;
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
if (PluginManager::auditPlugin()) {
foreach ($vars as $key => $value) {
if (isset($this->$key) && ($this->$key != $value)) {
$type = array('type' => 'edited', 'data' => array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => $key));
$type = array('type' => 'edited', 'key' => $key);
Signal::send('object.edited', $this, $type);
}
}
@@ -327,8 +327,6 @@ static function getSupportedMatchFields() {
}
function update($vars,&$errors) {
global $thisstaff;
//validate filter actions before moving on
if (!self::validate_actions($vars, $errors))
return false;
@@ -362,10 +360,10 @@ function update($vars,&$errors) {
$vars['target'] = 'Email';
}
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
if (PluginManager::auditPlugin()) {
foreach ($vars as $key => $value) {
if (isset($this->$key) && ($this->$key != $value) && $key != 'rules' && $key != 'actions') {
$type = array('type' => 'edited', 'data' => array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => $key));
$type = array('type' => 'edited', 'key' => $key);
Signal::send('object.edited', $this, $type);
}
}
@@ -398,8 +398,6 @@ function getLocal($subtag) {
}
function update($vars, &$errors) {
global $thisstaff;
$required = array();
if ($this->isEditable())
$required = array('name');
@@ -409,8 +407,8 @@ function update($vars, &$errors) {
$errors[$f] = sprintf(__('%s is required'), mb_convert_case($f, MB_CASE_TITLE));
elseif (isset($vars[$f])) {
if ($vars[$f] != $this->get($f)) {
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
$type = array('type' => 'edited', 'data' => array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => $f));
if (PluginManager::auditPlugin()) {
$type = array('type' => 'edited', 'key' => $f);
Signal::send('object.edited', $this, $type);
}
$this->set($f, $vars[$f]);
@@ -234,8 +234,8 @@ function getSubMenus(){
switch(strtolower($k)){
case 'dashboard':
$subnav[]=array('desc'=>__('System Logs'),'href'=>'logs.php','iconclass'=>'logs');
if (PluginManager::getPluginByName('View auditing for tickets', true))
$subnav[]=array('desc'=>__('Audit Logs'),'href'=>'audits.php','iconclass'=>'open');
if (PluginManager::auditPlugin())
$subnav[]=array('desc'=>__('Audit Logs'),'href'=>'audits.php','iconclass'=>'lists');
$subnav[]=array('desc'=>__('Information'),'href'=>'system.php','iconclass'=>'preferences');
break;
case 'settings':
@@ -374,8 +374,6 @@ static function supportsCustomData() {
}
function update($vars, &$errors) {
global $thisstaff;
$valid = true;
$forms = $this->getForms($vars);
foreach ($forms as $entry) {
@@ -421,24 +419,22 @@ function update($vars, &$errors) {
return false;
foreach ($this->getDynamicData() as $entry) {
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
if (PluginManager::auditPlugin()) {
$fields = $entry->getFields();
foreach ($fields as $field) {
$changes = $field->getChanges();
if ((is_array($changes) && $changes[0]) || $changes && !is_array($changes)) {
$type = array('type' => 'edited', 'data' =>
array('name' => $this->getName(),'person' => $thisstaff->getName()->name, 'key' => $field->getLabel()));
$type = array('type' => 'edited', 'key' => $field->getLabel());
Signal::send('object.edited', $this, $type);
}
}
}
if ($entry->getDynamicForm()->get('type') == 'O'
&& ($name = $entry->getField('name'))
) {
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
if (PluginManager::auditPlugin()) {
if ($this->name != $name->getClean()) {
$type = array('type' => 'edited', 'data' =>
array('name' => $this->getName(),'person' => $thisstaff->getName()->name, 'key' => 'Name'));
$type = array('type' => 'edited', 'key' => 'Name');
Signal::send('object.edited', $this, $type);
}
}
@@ -450,7 +446,7 @@ function update($vars, &$errors) {
$this->updated = SqlFunction::NOW();
}
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
if (PluginManager::auditPlugin()) {
if (($this->autoAddMembersAsCollabs() && !$vars['collab-all-flag']) ||
(!$this->autoAddMembersAsCollabs() && $vars['collab-all-flag'])) {
$auditCollabAll = true;
@@ -465,28 +461,32 @@ function update($vars, &$errors) {
(!$this->autoAssignAccountManager() && $vars['assign-am-flag'])) {
$auditAssignAm = true;
$key = 'assign-am-flag';
}
if ((!$this->shareWithPrimaryContacts() && $vars['sharing'] == 'sharing-primary' ||
(!$this->shareWithEverybody() && $vars['sharing'] == 'sharing-all' ||
($this->shareWithPrimaryContacts() && !$vars['sharing']) ||
($this->shareWithEverybody() && !$vars['sharing']))))
$sharing = true;
}
if ($auditCollabAll || $auditCollabPc || $auditAssignAm) {
$type = array('type' => 'edited', 'data' =>
array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => $key));
$type = array('type' => 'edited', 'key' => $key);
Signal::send('object.edited', $this, $type);
}
foreach ($vars as $key => $value) {
if ($key != 'id' && $this->get($key) && $value != $this->get($key)) {
$type = array('type' => 'edited', 'data' =>
array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => $key));
$type = array('type' => 'edited', 'key' => $key);
Signal::send('object.edited', $this, $type);
}
}
}
if ((!$this->shareWithPrimaryContacts() && $vars['sharing'] == 'sharing-primary') ||
($this->shareWithPrimaryContacts() && !$vars['sharing'])) {
$sharingPrimary = true;
$key = 'sharing-primary';
}
if ((!$this->shareWithEverybody() && $vars['sharing'] == 'sharing-all') ||
($this->shareWithEverybody() && !$vars['sharing'])) {
$sharingEverybody = true;
$key = 'sharing-all';
}
// Set flags
foreach (array(
'collab-all-flag' => Organization::COLLAB_ALL_MEMBERS,
@@ -503,10 +503,9 @@ function update($vars, &$errors) {
'sharing-primary' => Organization::SHARE_PRIMARY_CONTACT,
'sharing-all' => Organization::SHARE_EVERYBODY,
) as $ck=>$flag) {
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
if ($sharing) {
$type = array('type' => 'edited', 'data' =>
array('name' => $this->getName(), 'person' => $thisstaff->getName()->name, 'key' => 'sharing'));
if (PluginManager::auditPlugin()) {
if (($sharingPrimary || $sharingEverybody) && $key == $ck) {
$type = array('type' => 'edited', 'key' => 'sharing'); //adriane
Signal::send('object.edited', $this, $type);
}
}
@@ -598,8 +597,6 @@ static function getLink($id) {
}
static function fromVars($vars) {
global $thisstaff;
$vars['name'] = Format::striptags($vars['name']);
if (!($org = static::lookup(array('name' => $vars['name'])))) {
$org = static::create(array(
@@ -611,8 +608,8 @@ static function fromVars($vars) {
}
Signal::send('organization.created', $org);
if (PluginManager::getPluginByName('View auditing for tickets', true)) {
$type = array('type' => 'created', 'data' => array('name' => $org->getName(), 'person' => $thisstaff->getName()->name));
if (PluginManager::auditPlugin()) {
$type = array('type' => 'created');
Signal::send('object.created', $org, $type);
}
return $org;

0 comments on commit a4bd53b

Please sign in to comment.
You can’t perform that action at this time.