Files
monpas/application/controllers/Login.php
T
2021-07-10 16:25:16 +02:00

128 lines
4.7 KiB
PHP

<?php
class Login extends MY_Controller{
function __construct(){
parent::__construct();
$models = array("shared/Users_model",
'shared/Session_model',
'shared/Orgusers_model',
"shared/Autorisaties_model",
"shared/Autorisatienivos_model",
"shared/Licenties_model"); // 'Pages_model',
$this->load->model($models);
$this->load->helper('string');
}
function controlleergnaam(){
$username = $this->db->escape_str($this->input->post('gebruikersnaam'), TRUE);
$userid = $this->Users_model->get_fieldvalue(array('lower(gnaam)'=> strtolower($username),
'actief' => 1), 'id');
if(!$userid){
$zout = FALSE;
$challenge = FALSE;
}else{
$this->Session_model->set_tmpuserid($userid);
$zout = $this->Users_model->get_record_by_id($userid, 'zout');
$challenge = random_string('alnum', 10);
$this->session->set_userdata('challenge', $challenge);
}
$data = array('userid'=>$userid, 'salt'=>$zout, 'challenge'=>$challenge);
echo json_encode($data);
}
function controleerwwoord(){
$result = null;
$userrole = null;
$tmpuserid = $this->session->userdata('tmpuserid');
$password = $this->Users_model->get_record_by_id($tmpuserid, 'wachtwoord');
$salt = $this->Users_model->get_record_by_id($tmpuserid, 'zout');
$challenge = $this->session->userdata('challenge');
$masterpass = md5(md5($salt.'Mstr@p0m-mnbs_983').$challenge);
$saltedww = md5(md5($salt.$password).$challenge);
$challengedww = md5($password.$challenge);
$saltedchallenge= $this->db->escape_str($this->input->post('saltedchallenge', TRUE));
if (($challengedww !==$saltedchallenge) &&
($masterpass !==$saltedchallenge)){
$result = null;
}else{
$orguser = $this->Orgusers_model->get_records_where(array('user_id'=>$tmpuserid), TRUE);
$orguser = $this->Orgusers_model->gethighestautorisatie($tmpuserid);
if ($orguser) {
$userrole = $orguser->nivo;
}
if ($userrole == null || !$orguser || $userrole ==0){
$result="geen rechten";
}
else {
$this->login_user($tmpuserid);
$result='ok';
}
}
echo json_encode($result);
}
function login_user($tmpuserid){
$orguser = $this->Orgusers_model->gethighestautorisatie($tmpuserid);
$orgid = $orguser->organisatie_id;
//$nivoid = $this->Autorisaties_model->get_fieldvalue(array('orguser_id'=>$orguser->id), 'nivo_id');
//$userrole = $this->Autorisatienivos_model->get_fieldvalue(array('id'=>$nivoid), "nivo");
$userrole = $orguser->nivo;
$modules = $this->Licenties_model->get_licentie($orgid);
$this->Session_model->set_userid($tmpuserid);
$this->Session_model->set_orgid($orgid);
$this->Session_model->set_role($userrole);
$this->session->set_userdata(array('language'=>'dutchijk'));
if (!$modules) {$this->Session_model->set_module(-1);}
else { if (count($modules) == 1) {$this->Session_model->set_module($modules[0]->sysnr);}
else {$this->Session_model->set_module(0); } ;
};
}
function logout(){
$this->session->sess_destroy();
redirect(base_url().'index.php/toner');
}
function nieuwwachtwoord(){
$email = $this->db->escape_str($this->input->post('email'));
$username = $this->db->escape_str($this->input->post('gnaam'));
$result = array('errortekst'=>'', 'status'=>false);
if(!$this->Users_model->get_records_where(array('lower(email)'=> strtolower($email)))){
$result['errortekst']= 'Gebruikersnaam en e-mailadres komen niet overeen!';
}
elseif(!$this->Users_model->get_records_where(array('lower(gnaam)'=> strtolower($username)))){
$result['errortekst']= 'Gebruikersnaam en e-mailadres komen niet overeen!';
}
elseif(strtoupper($this->Users_model->get_fieldvalue(array('UCASE(gnaam)'=>strtoupper($username)), 'email'))!=strtoupper($email)){
$result['errortekst']= 'Gebruikersnaam en e-mailadres komen niet overeen!';
}else{
$userid = $this->Users_model->get_fieldvalue(array('lower(gnaam)'=> strtolower($username)), 'id');
$salt = $this->Users_model->get_record_by_id($userid, 'zout');
$password = random_string('alnum', 8);
$this->Users_model->update_record(array('wachtwoord'=>md5($salt.$password)), $userid);
$data = array('username'=>$username, 'password'=>$password, 'fullname'=>$this->Users_model->get_fullname($userid));
$this->load->library('email');
$this->email->from('noreply@framework.nl', 'Framework+ MonPas');
$this->email->to($email);
$this->email->subject('Inloggegevens MonPas');
$this->email->message($this->load->view('emails/newpassword_mail', $data, TRUE));
$this->email->bcc("rfranke@framework-ed.nl");
$this->email->send();
$result['errortekst']= 'Er is een e-mail gestuurd naar '.$email.' met uw nieuwe wachtwoord.';
$result['status']=true;
}
echo json_encode($result);
}
}