forked from MISP/MISP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/user login profiles2 (MISP#9379)
* new: [userloginprofiles] start over with previous code * fix: [user_login_profiles] fixes catching up the backlog * chg: [userloginprofile] email to org_admin for suspicious login * chg: [userloginprofile] only inform new device * chg: [userloginprofiles] view_login_history instead of view_auth_history * chg: [userloginprofile] make login history visually better * chg: [userloginprofile] inform admins of malicious report * fix: [userloginprofile] cleanup * fix: [userloginprofile] fixes Attribute include in Console * fix: [userloginprofile] db schema and changes * chg: [CI] log emails * chg: [PyMISP] branch change * chg: [test] test * fix: [userloginprofile] unique rows * fix: [userloginprofile] unique rows * chg: [cleanup] * Revert "chg: [PyMISP] branch change" This reverts commit 3f6fb46. * fix: [userloginprofile] fix worksers with monolog=1.25 browcap=5.1 * fix: [db] dump schema version * fix: [CI] newer php versions * fix: [composer] php version * fix: [php] revert to normal php7.4 tests --------- Co-authored-by: iglocska <[email protected]>
- Loading branch information
1 parent
18625a9
commit 7e2cb89
Showing
26 changed files
with
36,744 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
App::uses('AppController', 'Controller'); | ||
|
||
/** | ||
* @property UserLoginProfile $UserLoginProfile | ||
*/ | ||
class UserLoginProfilesController extends AppController | ||
{ | ||
public $components = array( | ||
'CRUD', | ||
'RequestHandler' | ||
); | ||
|
||
public $paginate = array( | ||
'limit' => 60, | ||
'order' => array( | ||
'UserLoginProfile.created_at' => 'DESC', | ||
) | ||
); | ||
|
||
public function index($user_id = null) | ||
{ | ||
$delete_buttons = false; | ||
// normal user | ||
$conditions = ['user_id' => $this->Auth->user('id')]; | ||
// org admin can see people from their own org | ||
if (!$this->_isSiteAdmin() && $this->_isAdmin()) { | ||
$conditions = ['User.org_id' => $this->Auth->user('org_id'), | ||
'user_id' => $user_id]; | ||
$delete_buttons = true; | ||
} | ||
// full admin can see all users | ||
else if ($this->_isSiteAdmin()) { | ||
$conditions = ['user_id' => $user_id]; | ||
$delete_buttons = true; | ||
} | ||
$this->CRUD->index([ | ||
'conditions' => $conditions | ||
]); | ||
if ($this->IndexFilter->isRest()) { | ||
return $this->restResponsePayload; | ||
} | ||
$this->set('title_for_layout', __('UserLoginProfiles')); | ||
$this->set('menuData', [ | ||
'menuList' => $this->_isSiteAdmin() ? 'admin' : 'globalActions', | ||
'menuItem' => 'authkeys_index', | ||
]); | ||
$this->set('delete_buttons', $delete_buttons); | ||
} | ||
|
||
/** | ||
* @param int|array $id | ||
* @return array | ||
* @throws NotFoundException | ||
*/ | ||
private function __deleteFetchConditions($id) | ||
{ | ||
if (empty($id)) { | ||
throw new NotFoundException(__('Invalid userloginprofile')); | ||
} | ||
$conditions = ['UserLoginProfile.id' => $id]; | ||
if ($this->_isSiteAdmin()) { | ||
// no additional filter for siteadmins | ||
} | ||
else if ($this->_isAdmin()) { | ||
$conditions['User.org_id'] = $this->Auth->user('org_id'); // org admin | ||
} | ||
else { | ||
$conditions['UserLoginProfile.user_id'] = $this->Auth->user('id'); // normal user | ||
} | ||
return $conditions; | ||
} | ||
|
||
public function admin_delete($id) | ||
{ | ||
if ($this->request->is('post') || $this->request->is('delete')) { | ||
$profile = $this->UserLoginProfile->find('first', array( | ||
'conditions' => $this->__deleteFetchConditions($id), // only allow (org/site) admins or own user to delete their data | ||
'fields' => ['UserLoginProfile.*'] | ||
)); | ||
if (empty($profile)) { | ||
throw new NotFoundException(__('Invalid UserLoginProfile')); | ||
} | ||
if ($this->UserLoginProfile->delete($id)) { | ||
$this->loadModel('Log'); | ||
$fieldsDescrStr = 'UserLoginProfile (' . $id . '): deleted'; | ||
$this->Log->createLogEntry($this->Auth->user(), 'delete', 'UserLoginProfile', $id, $fieldsDescrStr, json_encode($profile)); | ||
|
||
if ($this->_isRest()) { | ||
return $this->RestResponse->saveSuccessResponse('UserLoginProfile', 'admin_delete', $id, $this->response->type(), 'UserLoginProfile deleted.'); | ||
} else { | ||
$this->Flash->success(__('UserLoginProfile deleted')); | ||
$this->redirect(array('admin'=> false, 'controller' => 'userLoginProfiles', 'action' => 'index', $profile['UserLoginProfile']['user_id'])); | ||
} | ||
} | ||
$this->Flash->error(__('UserLoginProfile was not deleted')); | ||
$this->redirect(array('admin'=> false, 'controller' => 'userLoginProfiles', 'action' => 'index', $profile['UserLoginProfile']['user_id'])); | ||
} | ||
} | ||
|
||
public function trust($logId) | ||
{ | ||
if ($this->request->is('post')) { | ||
$this->__setTrust($logId, 'trusted'); | ||
} | ||
$this->redirect(array('controller' => 'users', 'action' => 'view_login_history')); | ||
} | ||
|
||
public function malicious($logId) | ||
{ | ||
if ($this->request->is('post')) { | ||
$userLoginProfile = $this->__setTrust($logId, 'malicious'); | ||
$this->Flash->info(__('You marked a login suspicious. You must change your password NOW !')); | ||
$this->loadModel('Log'); | ||
$details = 'User reported suspicious login for log ID: '. $logId; | ||
// raise an alert (the SIEM component should ensure (org)admins are informed) | ||
$this->Log->createLogEntry($this->Auth->user(), 'auth_alert', 'User', $this->Auth->user('id'), 'Suspicious login reported.', $details); | ||
// inform (org)admins of the report, they might want to action this... | ||
$user = $this->User->find('first', array( | ||
'conditions' => array( | ||
'User.id' => $this->Auth->user('id') | ||
), | ||
'recursive' => -1 | ||
)); | ||
unset($user['User']['password']); | ||
$this->UserLoginProfile->email_report_malicious($user, $userLoginProfile); | ||
// change account info to force password change, redirect to new password page. | ||
$this->User->id = $this->Auth->user('id'); | ||
$this->User->saveField('change_pw', 1); | ||
$this->redirect(array('controller' => 'users', 'action' => 'change_pw')); | ||
return; | ||
} | ||
$this->redirect(array('controller' => 'users', 'action' => 'view_login_history')); | ||
} | ||
|
||
private function __setTrust($logId, $status) | ||
{ | ||
$user = $this->Auth->user(); | ||
$this->loadModel('Log'); | ||
$log = $this->Log->find('first', array( | ||
'conditions' => array( | ||
'Log.user_id' => $user['id'], | ||
'Log.id' => $logId, | ||
'OR' => array ('Log.action' => array('login', 'login_fail', 'auth', 'auth_fail')) | ||
), | ||
'fields' => array('Log.action', 'Log.created', 'Log.ip', 'Log.change', 'Log.id'), | ||
'order' => array('Log.created DESC') | ||
)); | ||
$data = $this->UserLoginProfile->_fromLog($log['Log']); | ||
if (!$loginProfile) return $data; // skip if empty logs | ||
$data['status'] = $status; | ||
$data['user_id'] = $user['id']; | ||
$data['hash'] = $this->UserLoginProfile->hash($data); | ||
|
||
// add the userLoginProfile trust status if it not already there, based on the hash | ||
$result = $this->UserLoginProfile->find('count', array( | ||
'conditions' => array('UserLoginProfile.hash' => $data['hash']) | ||
)); | ||
if ($result == 0) { | ||
// no row yet, save it. | ||
$this->UserLoginProfile->save($data); | ||
} | ||
return $data; | ||
} | ||
|
||
} |
Oops, something went wrong.