436 lines
14 KiB
PHP
436 lines
14 KiB
PHP
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
|
|
use Mailgun\Mailgun;
|
|
|
|
class Users extends MY_Controller{
|
|
|
|
function __construct(){
|
|
parent::__construct();
|
|
if ( ! $this->userid) exit('geen toegang');
|
|
$models = array( "shared/Users_model",
|
|
'shared/Organisaties_model',
|
|
"shared/Orgusers_model",
|
|
"shared/functies_model",
|
|
"monpas/Filledversions_model",
|
|
"monpas/Groepenxusers_model",
|
|
"monpas/Versies_model",
|
|
"monpas/Orgversies_model");
|
|
$this->load->model($models);
|
|
$this->load->helper('string');
|
|
if ( ! $this->check_online_status() ) {
|
|
echo json_encode( array('offline' => TRUE) );
|
|
die;
|
|
};// if
|
|
}
|
|
function getuserdetail(){
|
|
if (!$this->userid || $this->nologin) die;
|
|
|
|
$userid = $this->uri->segment(4);
|
|
if (!$userid) {$userid = $this->userid; };
|
|
$user = $this->Users_model->getdetails($userid);
|
|
echo json_encode($user);
|
|
}
|
|
|
|
public function updateuser(){
|
|
//TODO sanitize input
|
|
if (!$this->userid || $this->nologin){die;};
|
|
|
|
if ($this->Autorisaties_model->check_autorisatie($this->userrole, WRITER) > 0 ) {
|
|
$data=array('voornaam' => $this->clean_input('voornaam'),
|
|
'tussenvoegsel' => $this->clean_input('tussenvoegsel'),
|
|
'achternaam' => $this->clean_input('achternaam'),
|
|
'email' => $this->clean_input('email') ,
|
|
'twofactor' => $this->clean_input('twofactor') );
|
|
if (strlen($this->input->post('wachtwoord')) >=4){
|
|
$data['wachtwoord']=$this->input->post('wachtwoord');
|
|
} ;
|
|
$this->Users_model->update_record($data, $this->userid);
|
|
};
|
|
$fullname = $this->Users_model->get_fullname($this->userid);
|
|
echo json_encode($fullname);
|
|
}
|
|
|
|
function getorgs(){
|
|
$userid = $this->userid;
|
|
$userorg = $this->Organisaties_model->getorg($this->globorgid) ;
|
|
$orgs = null;
|
|
if ($userorg->soort == 4){
|
|
// doe niks
|
|
}
|
|
if ($userorg->soort == 3){
|
|
//get scholen
|
|
$userorg->orgs1= $this->Organisaties_model->getorgsvanaf($userorg->id);
|
|
};
|
|
if($userorg->soort==2){
|
|
$userorg->orgs1= $this->Organisaties_model->getorgsvanaf($userorg->id);
|
|
if ($userorg->orgs1){
|
|
foreach($userorg->orgs1 as $bg){
|
|
$bg->orgs2 = $this->Organisaties_model->getorgsvanaf($bg->id);
|
|
}
|
|
}
|
|
}
|
|
if ($userorg->soort=1){
|
|
$userorg->orgs= $this->Organisaties_model->getorgsvanaf($userorg->id);
|
|
if ($userorg->orgs){
|
|
foreach($userorg->orgs as $kamer){
|
|
$kamer->orgs = $this->Organisaties_model->getorgsvanaf($kamer->id);
|
|
if ($kamer->orgs){
|
|
foreach($kamer->orgs as $bg){
|
|
$bg->orgs = $this->Organisaties_model->getorgsvanaf($bg->id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
echo json_encode($userorg);
|
|
}
|
|
|
|
|
|
function getorggebruikers(){
|
|
if (!$this->nologin){
|
|
$orgid = $this->uri->segment(4);
|
|
$users = $this->Users_model->getorggebruikers($orgid, false);
|
|
|
|
echo json_encode($users);
|
|
}
|
|
}
|
|
|
|
function adduser(){
|
|
//TODO clean input
|
|
if ($this->Autorisaties_model->check_autorisatie($this->userrole, WRITER) > 0 ) {
|
|
$ok = true;
|
|
$errortekst = '';
|
|
$user = null;
|
|
$orgid = $this->input->post("orgid");
|
|
$gnaam=$this->input->post('gnaam');
|
|
$gnaam = trim($gnaam);
|
|
if ($this->Users_model->check_username($gnaam) > 0){
|
|
$errortekst =" Gebruikersnaam is niet toegestaan. Gebruik een andere naam.";
|
|
$ok = false;
|
|
};
|
|
if ($ok) {
|
|
// maak een user
|
|
//maak wachtwoord en send email
|
|
$zout = random_string('alnum', 15);
|
|
$ww = random_string('alnum', 8);
|
|
$saltedww = md5($zout.$ww);
|
|
$data = array("voornaam" =>$this->input->post('voornaam'),
|
|
"tussenvoegsel" =>$this->input->post('tussenvoegsel'),
|
|
"achternaam" =>$this->input->post('achternaam'),
|
|
"email" =>$this->input->post('email'),
|
|
"gnaam" =>$gnaam,
|
|
"zout" =>$zout,
|
|
"wachtwoord" =>$saltedww);
|
|
$userid = $this->Users_model->insert_record($data);
|
|
$this->sendmail($userid,$ww,$orgid);
|
|
// maak een orguser
|
|
$data = array( "user_id" => $userid,
|
|
"organisatie_id" => $orgid);
|
|
$orguserid = $this->Orgusers_model->insert_record($data);
|
|
//maak autorisatie
|
|
$data = array( "nivo_id" =>$this->input->post('autorisatienivoid'),
|
|
"orguser_id" =>$orguserid);
|
|
$this->Autorisaties_model->insert_record($data);
|
|
//get user gegevens om op te nemen in de view
|
|
$user=$this->Users_model->getorggebruikers($orgid, $userid);
|
|
|
|
} // if ok
|
|
}
|
|
else {
|
|
$ok =false;
|
|
$errortekst ="U bent niet geautoriseerd voor deze handeling.";
|
|
$user ='';
|
|
}; // if geen writer
|
|
echo json_encode(array('ok' =>$ok,
|
|
'errortekst' => $errortekst,
|
|
'user' => $user));
|
|
}
|
|
|
|
function getBijlage($orgid){
|
|
|
|
$org = $this->Organisaties_model->get(['id'=>$orgid],false, true);
|
|
|
|
if (! $org->randomname && $org->id != $org->valtonder_id){
|
|
return $this->getBijlage($org->valtonder_id);
|
|
}
|
|
else return $org;
|
|
|
|
}
|
|
|
|
function sendmail($userid, $ww, $orgid){
|
|
$user = $this->Users_model->get_record_by_id($userid);
|
|
$data = array('username'=>$user->gnaam, 'password'=>$ww, 'fullname'=>$this->Users_model->get_fullname($userid));
|
|
$org = $this->getBijlage($orgid);
|
|
|
|
$html = $this->load->view('emails/newuser_mail', $data, TRUE);
|
|
|
|
// if( $_ENV['ENVIRONMENT'] == 'production')
|
|
// {
|
|
// //Deze code wordt alleen op produktie gebruikt en stuurt de mail
|
|
// //naar Mailgun via een API (Codeigniter email library wordt niet gebruikt
|
|
// $mg = Mailgun::create( $_ENV['MAIL']['APIKEY'] );
|
|
//
|
|
// $mailData = [
|
|
// 'from' => 'info@monpas.nl',
|
|
// 'to' => $user->email,
|
|
// 'cc' => 'rfranke@framework-ed.nl',
|
|
// 'subject' => 'Inloggegevens MonPas',
|
|
// 'html' => $html,
|
|
// 'inline' => [
|
|
// [
|
|
// 'filename' => 'monpasklein.png',
|
|
// 'filePath' => FCPATH . 'css/images/monpasklein.png']
|
|
// ]
|
|
// ];
|
|
//
|
|
// if( $org->randomname ) $mailData['attachment'][0] = [
|
|
// 'filename' => $org->bijlagefile,
|
|
// 'filePath' => MYROOTPATH. 'docs/'.$org->randomname ];
|
|
//
|
|
// $mg->messages()->send( $_ENV['MAIL']['DOMAIN'], $mailData );
|
|
// }else{
|
|
$this->load->library('email');
|
|
|
|
$this->email->from('noreply@framework.nl', 'Framework+ MonPas');
|
|
$this->email->to($user->email);
|
|
$this->email->subject('Inloggegevens MonPas');
|
|
$this->email->message($this->load->view('emails/newuser_mail', $data, TRUE));
|
|
$this->email->bcc("rfranke@framework-ed.nl");
|
|
if ($org->randomname)
|
|
$this->email->attach('./../docs/'.$org->randomname,'attachment', $org->bijlagefile);
|
|
$this->email->send();
|
|
// }
|
|
}
|
|
|
|
function updateuser2(){
|
|
// TODO sanitize input
|
|
if ($this->Autorisaties_model->check_autorisatie($this->userrole, WRITER) > 0 ) {
|
|
$ok = true;
|
|
$errortekst = '';
|
|
$user = null;
|
|
$fullname ='';
|
|
$userid = $this->input->post("id");
|
|
$orgid = $this->input->post("orgid");
|
|
$gnaam = trim($this->input->post('gnaam'));
|
|
// controleer gebruikersnaam
|
|
if ($this->Users_model->check_username($gnaam, $userid) > 0){
|
|
$errortekst =" Gebruikersnaam is niet toegestaan. Gebruik een andere naam.";
|
|
$ok = false;
|
|
};
|
|
|
|
if ($ok) {
|
|
$data = array("voornaam" =>$this->input->post('voornaam'),
|
|
"tussenvoegsel" =>$this->input->post('tussenvoegsel'),
|
|
"achternaam" =>$this->input->post('achternaam'),
|
|
"email" =>$this->input->post('email'),
|
|
"twofactor" =>$this->clean_input('twofactor'),
|
|
"gnaam" =>$gnaam);
|
|
$this->Users_model->update_record($data, $userid);
|
|
|
|
//update autorisatie
|
|
$where = array('user_id' =>$userid, 'organisatie_id'=>$orgid);
|
|
$orguserid = $this->Orgusers_model->get_records_where($where, true);
|
|
$orguserid = $orguserid->id;
|
|
$data = array( "nivo_id" =>$this->input->post('autorisatienivoid'));
|
|
$where = array('orguser_id' => $orguserid);
|
|
$this->Autorisaties_model->update_record_where($data, $where);
|
|
//get user gegevens om op te nemen in de view
|
|
$user=$this->Users_model->getorggebruikers($orgid, $userid);
|
|
// als de veranderde user = glob user : pas fullname onder menu aan
|
|
if ($userid == $this->userid){
|
|
$fullname = $this->Users_model->get_fullname($userid);
|
|
}
|
|
if ($this->input->post("status") == 3){
|
|
//stuur email aan de gebruiker
|
|
$zout = random_string('alnum', 15);
|
|
$ww = random_string('alnum', 8);
|
|
$saltedww = md5($zout.$ww);
|
|
$data=array('zout' => $zout, "wachtwoord" =>$saltedww);
|
|
$this->Users_model->update_record($data, $userid);
|
|
$this->sendmail($userid,$ww,$orgid);
|
|
}
|
|
} // if ok
|
|
|
|
//get user gegevens om op te nemen in de view
|
|
$user=$this->Users_model->getorggebruikers($orgid, $userid);
|
|
}
|
|
else {$ok = false;
|
|
$errortekst = "U bent niet geautoriseerd voor deze handeling." ;
|
|
$user = '';
|
|
$fullname = '';
|
|
}; // geen write rechten
|
|
echo json_encode(array('ok' => $ok,
|
|
'errortekst' => $errortekst,
|
|
'user' => $user,
|
|
'fullname' => $fullname));
|
|
}
|
|
function deleteuser(){
|
|
//TODO sanitize input
|
|
$user=FALSE;
|
|
$userid= $this->uri->segment(4);
|
|
$orgid= $this->uri->segment(5);
|
|
$orguser = $this->Orgusers_model->get_records_where(array("user_id"=>$userid, "organisatie_id"=>$orgid),true);
|
|
$ok = true;
|
|
$errortekst = '';
|
|
|
|
if ($this->Autorisaties_model->check_autorisatie($this->userrole, WRITER) <=0 ) {
|
|
$ok = false;
|
|
$errortekst = "U bent niet geautoriseerd tot verwijderen.";
|
|
}
|
|
elseif ($userid == $this->userid){
|
|
$ok = false;
|
|
$errortekst .=" U mag de huidige gebruiker niet verwijderen.";
|
|
}
|
|
elseif ($this->Filledversions_model->check_for_record(array("user_id"=>$userid , "organisatie_id"=>$orgid)) ||
|
|
$this->Groepenxusers_model->check_for_record(array("orguser_id"=>$orguser->id)) ){
|
|
$errortekst .="Er zijn invulgegevens van deze gebruiker; en/of groepen gekoppeld aan deze gebruiker. Gebruiker is gearchiveerd";
|
|
$this->Autorisaties_model->update_record_where(array("nivo_id"=>100),array("orguser_id"=>$orguser->id));
|
|
$user = $this->Users_model->getorggebruikers($orgid,$userid);
|
|
}
|
|
else {
|
|
$orguser = $this->Orgusers_model->get_records_where(array("user_id" => $userid, "organisatie_id"=>$orgid), true);
|
|
$this->Autorisaties_model->delete_record_where(array("orguser_id"=>$orguser->id));
|
|
$this->Orgusers_model->delete_record($orguser->id);
|
|
|
|
// wordt de user nog bij een andere organisatie gebruikt?
|
|
$orguser=$this->Orgusers_model->get_records_where(array("user_id"=>$userid));
|
|
if (!$orguser){
|
|
$this->Users_model->delete_record($userid);
|
|
}
|
|
}
|
|
echo json_encode(array('ok'=>$ok, 'errortekst'=>$errortekst,'user'=>$user));
|
|
}
|
|
public function savestatus(){
|
|
// TODO sanitize input
|
|
if ($this->Autorisaties_model->check_autorisatie($this->userrole, SUPERUSER) > 0){
|
|
$userid =$this->clean_input('userid');
|
|
$status =$this->clean_input('status');
|
|
$this->Users_model->update_record(array("actief"=>$status), $userid);
|
|
}
|
|
}
|
|
function SendNewWW(){
|
|
//TODO sanitize input
|
|
$userid = $this->clean_input('userid');
|
|
$orgid = $this->clean_input('orgid');
|
|
$ok = true;
|
|
$errortekst = '';
|
|
|
|
$user = $this->Users_model->get_record_by_id($userid);
|
|
$ww = random_string('alnum', 8);
|
|
$saltedww = md5($user->zout.$ww);
|
|
// update user
|
|
$this->Users_model->update_record(array('wachtwoord'=>$saltedww),$userid);
|
|
$data = array('username'=>$user->gnaam, 'password'=>$ww, 'fullname'=>$this->Users_model->get_fullname($userid));
|
|
|
|
$html = $this->load->view( 'emails/newpassword_mail', $data, TRUE );
|
|
$org = $this->getBijlage($orgid);
|
|
|
|
// if( $_ENV['ENVIRONMENT'] == 'production')
|
|
// {
|
|
// //Deze code wordt alleen op produktie gebruikt en stuurt de mail
|
|
// //naar Mailgun via een API (Codeigniter email library wordt niet gebruikt
|
|
// $mg = Mailgun::create( $_ENV['MAIL']['APIKEY'] );
|
|
// $mailData = [
|
|
// 'from' => 'info@monpas.nl',
|
|
// 'to' => $user->email,
|
|
// 'cc' => 'rfranke@framework-ed.nl',
|
|
// 'subject' => 'Inloggegevens MonPas',
|
|
// 'html' => $html,
|
|
// 'inline' => [
|
|
// [
|
|
// 'filename' => 'monpasklein.png',
|
|
// 'filePath' => FCPATH . 'css/images/monpasklein.png']
|
|
// ]
|
|
// ];
|
|
// if( $org->randomname ) $mailData['attachment'][0] = [
|
|
// 'filename' => $org->bijlagefile,
|
|
// 'filePath' => MYROOTPATH. 'docs/'.$org->randomname ];
|
|
//
|
|
// $mg->messages()->send( $_ENV['MAIL']['DOMAIN'], $mailData );
|
|
// }else
|
|
// {
|
|
$this->load->library( 'email' );
|
|
|
|
$this->email->from( 'noreply@framework.nl', 'Framework+ MonPas' );
|
|
$this->email->to( $user->email );
|
|
$this->email->subject( 'Inloggegevens MonPas' );
|
|
$this->email->message( $html );
|
|
$this->email->bcc( "rfranke@framework-ed.nl" );
|
|
|
|
$this->email->attach(MYROOTPATH. 'docs/'.$org->randomname,'attachment', $org->bijlagefile);
|
|
$this->email->send();
|
|
$result = $this->email->print_debugger();
|
|
// }
|
|
|
|
echo json_encode(array('ok'=>$ok, 'errortekst'=>$errortekst));
|
|
}
|
|
|
|
function geteigenorgs($valtonderid){
|
|
|
|
}
|
|
private function getswv($orgid){
|
|
$organisatie =$this->Organisaties_model->get_record_by_id($orgid);
|
|
if ($organisatie) {
|
|
if ($organisatie->soort ==1 ) {
|
|
return $organisatie->id;
|
|
}
|
|
else{
|
|
$orgid = $this->getswv($organisatie->valtonder_id);
|
|
};
|
|
}
|
|
else {
|
|
return $orgid= null;
|
|
}
|
|
return $orgid;
|
|
}
|
|
|
|
function getorglist($orgid,$orglist){
|
|
|
|
$orgs = $this->Organisaties_model->getorgsvanaf($orgid);
|
|
if ($orgs){
|
|
|
|
foreach($orgs as $org){
|
|
if ($org->soort < 4 && $org->soort != 1){
|
|
$orglist .=$org->id.',';
|
|
$orglist=$this->getorglist($org->id,$orglist);
|
|
|
|
}
|
|
else {
|
|
$orglist.= $org->id.',';
|
|
}
|
|
}
|
|
}
|
|
return $orglist;
|
|
}
|
|
|
|
function getotherusers(){
|
|
//TODO clean input
|
|
// haal de gebruikers op van andere organisaties onder het SWV
|
|
$orgid= $this->uri->segment(4);
|
|
$swvid = $this->getswv($orgid);
|
|
$orgids='';
|
|
|
|
$orgids = $this->getorglist($swvid, $swvid.',');
|
|
|
|
$orgids=substr($orgids, 0, strlen($orgids) - 1);
|
|
$ousers= $this->Users_model->otherusers($orgid,$orgids);
|
|
echo json_encode($ousers);
|
|
}
|
|
function savecopyuser(){
|
|
//TODO clean input
|
|
$userid = $this->input->post("userid");
|
|
$orgid = $this->input->post("orgid");
|
|
$data = array( "user_id" => $userid,
|
|
"organisatie_id" => $orgid);
|
|
$orguserid = $this->Orgusers_model->insert_record($data);
|
|
//maak autorisatie
|
|
$data = array( "nivo_id" =>$this->input->post('autorisatie'),
|
|
"orguser_id" =>$orguserid);
|
|
$this->Autorisaties_model->insert_record($data);
|
|
//get user gegevens om op te nemen in de view
|
|
$user=$this->Users_model->getorggebruikers($orgid, $userid);
|
|
echo json_encode($user);
|
|
}
|
|
}
|