Skip to content

Commit

Permalink
Improved session management
Browse files Browse the repository at this point in the history
  • Loading branch information
bbalet committed Mar 23, 2014
1 parent 2db0ef2 commit 69b5ec0
Show file tree
Hide file tree
Showing 24 changed files with 412 additions and 152 deletions.
19 changes: 19 additions & 0 deletions application/controllers/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,35 @@

class Calendar extends CI_Controller {

/**
* Connected user fullname
* @var string $fullname
*/
private $fullname;

/**
* Connected user privilege
* @var bool true if admin, false otherwise
*/
private $is_admin;

public function __construct() {
parent::__construct();
//Check if user is connected
if (!$this->session->userdata('logged_in')) {
redirect('session/login');
}
$this->load->model('leaves_model');
$this->fullname = $this->session->userdata('firstname') . ' ' .
$this->session->userdata('lastname');
$this->is_admin = $this->session->userdata('is_admin');
}

public function team() {
$data['leaves'] = $this->leaves_model->get_leaves();
$data['title'] = 'My Leave Requests';
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('calendar/team', $data);
Expand All @@ -41,6 +58,8 @@ public function team() {
public function individual() {
$data['leaves'] = $this->leaves_model->get_leaves();
$data['title'] = 'My Leave Requests';
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('calendar/individual', $data);
Expand Down
76 changes: 51 additions & 25 deletions application/controllers/leaves.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,35 @@

class Leaves extends CI_Controller {

/**
* Connected user fullname
* @var string $fullname
*/
private $fullname;

/**
* Connected user privilege
* @var bool true if admin, false otherwise
*/
private $is_admin;

public function __construct() {
parent::__construct();
//Check if user is connected
if (!$this->session->userdata('logged_in')) {
redirect('session/login');
}

$this->fullname = $this->session->userdata('firstname') . ' ' .
$this->session->userdata('lastname');
$this->is_admin = $this->session->userdata('is_admin');
$this->load->model('leaves_model');
/*
//See: http://www.codeigniter.fr/user_guide/libraries/email.html
$this->load->library('email');
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->cc('[email protected]');
$this->email->bcc('[email protected]');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger(); */
}

public function index() {
$data['leaves'] = $this->leaves_model->get_leaves();
$data['title'] = 'My Leave Requests';
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('leaves/index', $data);
Expand All @@ -66,7 +60,9 @@ public function view($id) {
if (empty($data['leaves_item'])) {
show_404();
}
$data['title'] = 'User';
$data['title'] = 'Leave details';
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('leaves/view', $data);
Expand All @@ -77,6 +73,8 @@ public function create() {
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Request a leave';
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;

$this->form_validation->set_rules('startdate', 'Start Date', 'required');
$this->form_validation->set_rules('startdatetype', 'Start Date type', 'required');
Expand All @@ -93,6 +91,34 @@ public function create() {
$this->load->view('templates/footer');
} else {
$this->leaves_model->set_leaves();
$this->load->model('users_model');
$this->load->model('settings_model');
$manager = $this->users_model->get_users($this->session->userdata('manager'));

//Send an e-mail to the manager
//See: http://www.codeigniter.fr/user_guide/libraries/email.html
$this->load->library('email');
$config = $this->settings_model->get_mail_config();
$this->email->initialize($config);

$this->load->library('parser');
$data = array(
'Title' => 'Leave Request',
'Firstname' => $this->session->userdata('firstname'),
'Lastname' => $this->session->userdata('lastname'),
'StartDate' => $this->input->post('startdate'),
'EndDate' => $this->input->post('enddate')
);
$message = $this->parser->parse('emails/request', $data, TRUE);

$this->email->from('[email protected]', 'LMS');
$this->email->to($manager['email']);
$this->email->subject('[LMS] Leave Request from ' .
$this->session->userdata('firstname') . ' ' .
$this->session->userdata('lastname'));
$this->email->message($message);
$this->email->send();
//echo $this->email->print_debugger();
$this->index();
}
}
Expand Down Expand Up @@ -140,7 +166,7 @@ public function export() {
$line++;
}

/*//For debug purposes
/*//For debuging purposes
$filename = 'testFile.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="' . $filename . '"');
Expand Down
18 changes: 18 additions & 0 deletions application/controllers/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@

class Pages extends CI_Controller {

/**
* Connected user fullname
* @var string $fullname
*/
private $fullname;

/**
* Connected user privilege
* @var bool true if admin, false otherwise
*/
private $is_admin;

/**
* Default constructor
*/
Expand All @@ -29,6 +41,9 @@ public function __construct() {
if (!$this->session->userdata('logged_in')) {
redirect('session/login');
}
$this->fullname = $this->session->userdata('firstname') . ' ' .
$this->session->userdata('lastname');
$this->is_admin = $this->session->userdata('is_admin');
}

public function view($page = 'home') {
Expand All @@ -37,7 +52,10 @@ public function view($page = 'home') {
}

$data['title'] = ucfirst($page); // Capitalize the first letter
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('pages/' . $page, $data);
$this->load->view('templates/footer', $data);
}
Expand Down
137 changes: 25 additions & 112 deletions application/controllers/requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@

class Users extends CI_Controller {

/**
* Connected user fullname
* @var string $fullname
*/
private $fullname;

/**
* Connected user privilege
* @var bool true if admin, false otherwise
*/
private $is_admin;

/**
* Default constructor
*/
Expand All @@ -30,6 +42,9 @@ public function __construct() {
redirect('session/login');
}
$this->load->model('users_model');
$this->fullname = $this->session->userdata('firstname') . ' ' .
$this->session->userdata('lastname');
$this->is_admin = $this->session->userdata('is_admin');
}

/**
Expand All @@ -38,6 +53,8 @@ public function __construct() {
public function index() {
$data['users'] = $this->users_model->get_users();
$data['title'] = 'Users';
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('users/index', $data);
Expand All @@ -48,12 +65,14 @@ public function index() {
* Display details of a given user
* @param int $id User identifier
*/
public function view($id) {
public function accept($id, $comment="") {
$data['users_item'] = $this->users_model->get_users($id);
if (empty($data['users_item'])) {
show_404();
}
$data['title'] = 'User';
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('users/view', $data);
Expand All @@ -64,24 +83,21 @@ public function view($id) {
* Display a for that allows updating a given user
* @param int $id User identifier
*/
public function edit($id) {
public function reject($id, $comment="") {
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a new user';
$data['title'] = 'Update a leave request';
$data['fullname'] = $this->fullname;
$data['is_admin'] = $this->is_admin;

$this->form_validation->set_rules('firstname', 'Firstname', 'required');
$this->form_validation->set_rules('lastname', 'Lastname', 'required');
$this->form_validation->set_rules('firstname', 'Firstname', 'required');
$this->form_validation->set_rules('lastname', 'Lastname', 'required');
$this->form_validation->set_rules('login', 'Login identifier', 'required');
$this->form_validation->set_rules('email', 'E-mail', 'required');
$this->form_validation->set_rules('role', 'role', 'required');


$data['users_item'] = $this->users_model->get_users($id);
if (empty($data['users_item'])) {
show_404();
}
$data['title'] = 'User';
$this->load->model('roles_model');
$data['roles'] = $this->roles_model->get_roles();
$data['users'] = $this->users_model->get_users();
Expand All @@ -90,107 +106,4 @@ public function edit($id) {
$this->load->view('users/edit', $data);
$this->load->view('templates/footer');
}

/**
* Delete a given user
* @param int $id User identifier
*/
public function delete($id) {
//Test if user exists
$data['users_item'] = $this->users_model->get_users($id);
if (empty($data['users_item'])) {
show_404();
} else {
$this->users_model->delete_user($id);
}
$this->index();
}

/**
* Display the form / action Create a new user
*/
public function create() {
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a new user';

$this->load->model('roles_model');
$data['roles'] = $this->roles_model->get_roles();
$data['users'] = $this->users_model->get_users();
$data['public_key'] = file_get_contents('./assets/keys/public.pem', true);

$this->form_validation->set_rules('firstname', 'Firstname', 'required');
$this->form_validation->set_rules('lastname', 'Lastname', 'required');
$this->form_validation->set_rules('login', 'Login identifier', 'required');
$this->form_validation->set_rules('email', 'E-mail', 'required');
//$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('CipheredValue', 'Password', 'required');
$this->form_validation->set_rules('role', 'role', 'required');

if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('users/create');
$this->load->view('templates/footer');
} else {
$this->users_model->set_users();
$this->index();
}
}

/**
* Action : update a user (using data from HTTP form)
*/
public function update() {
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a new user';

$this->form_validation->set_rules('firstname', 'Firstname', 'required');
$this->form_validation->set_rules('lastname', 'Lastname', 'required');

if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);
$this->load->view('users/edit/' . $this->input->post('id'));
$this->load->view('templates/footer');
} else {
$this->users_model->update_users();
$this->index();
}
}

/**
* Action: export the list of all users into an Excel file
*/
public function export() {
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('List of users');
$this->excel->getActiveSheet()->setCellValue('A1', 'ID');
$this->excel->getActiveSheet()->setCellValue('B1', 'Firstname');
$this->excel->getActiveSheet()->setCellValue('C1', 'Lastname');
$this->excel->getActiveSheet()->getStyle('A1:C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('A1:C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

$users = $this->users_model->get_users();
$line = 2;
foreach ($users as $user) {
$this->excel->getActiveSheet()->setCellValue('A' . $line, $user['id']);
$this->excel->getActiveSheet()->setCellValue('B' . $line, $user['firstname']);
$this->excel->getActiveSheet()->setCellValue('C' . $line, $user['lastname']);
$line++;
}

$filename = 'users.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
}

//TODO reset password from list

//TODO reset my password as connected user
}
Loading

0 comments on commit 69b5ec0

Please sign in to comment.