Skip to content

Commit 3a8f252

Browse files
committed
User Module
1 parent fb7d5a4 commit 3a8f252

21 files changed

Lines changed: 768 additions & 509 deletions

File tree

application/config/autoload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
|
5959
| $autoload['libraries'] = array('user_agent' => 'ua');
6060
*/
61-
$autoload['libraries'] = array('session');
61+
$autoload['libraries'] = array('session','form_validation','pagination');
6262

6363
/*
6464
| -------------------------------------------------------------------
@@ -89,7 +89,7 @@
8989
|
9090
| $autoload['helper'] = array('url', 'file');
9191
*/
92-
$autoload['helper'] = array('url');
92+
$autoload['helper'] = array('url','view','util');
9393

9494
/*
9595
| -------------------------------------------------------------------

application/controllers/admin/Auth.php

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
use Model\User;
4+
5+
class Authentication extends CI_Controller {
6+
public function index(){
7+
$user_id = (int)$this->session->userdata('login_admin');
8+
if($user_id > 0){
9+
redirect(route('admin.dashboard'));
10+
}
11+
12+
$this->load->view('admin/auth/login');
13+
}
14+
15+
public function logout(){
16+
$this->session->unset_userdata('login_admin');
17+
redirect(route('admin.login'));
18+
}
19+
20+
public function check_login(){
21+
$json = array();
22+
23+
$this->form_validation->set_rules('email', 'Email Address', 'required');
24+
$this->form_validation->set_rules('password', 'Password', 'required');
25+
26+
$data = $this->input->post(NULL,true);
27+
if($this->form_validation->run() == FALSE){
28+
$json['errors'] = $this->form_validation->error_array();
29+
}
30+
31+
if (!isset($json['errors'])) {
32+
$user = User::where("email","like",$data['email'])->first();
33+
if(!$user){
34+
$json['errors']['email'] = 'Invalid Email Address or Password';
35+
} else if(!$user->status){
36+
$json['errors']['email'] = 'User must be active for login';
37+
} else if(!bcrypt_check($data['password'], $user->password)){
38+
$json['errors']['email'] = 'User must be active for login';
39+
} else {
40+
$this->session->set_userdata('login_admin', $user->id);
41+
$json['redirect'] = route("admin.dashboard");
42+
}
43+
}
44+
45+
View::json($json);
46+
}
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Dashboard extends CI_Controller {
5+
6+
public function index(){
7+
View::load('admin/common/dashboard');
8+
}
9+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
use Model\User;
4+
5+
class UserController extends CI_Controller {
6+
public function index($currentPage = 1){
7+
\Illuminate\Pagination\Paginator::currentPageResolver(function () use ($currentPage) {
8+
return $currentPage;
9+
});
10+
11+
$users = User::paginate(10);
12+
$config['base_url'] = route('admin.user.list');
13+
makePaginate($users, $config);
14+
15+
View::load('admin/user/index',compact(['users']));
16+
}
17+
18+
public function edit_form($user_id = 0){
19+
$user = User::findOrNew($user_id);
20+
View::load('admin/user/form',compact(['user']));
21+
}
22+
23+
public function submit_form($user_id = 0){
24+
$json = array();
25+
$data = $this->input->post(NULL,true);
26+
27+
$this->form_validation->set_rules('name', 'Name', 'required');
28+
$this->form_validation->set_rules('email', 'Email Address', 'required');
29+
$this->form_validation->set_rules('status', 'Status', 'required');
30+
31+
if((int)$user_id == 0 || (isset($data['password']) && $data['password']) ){
32+
$this->form_validation->set_rules('password', 'Password', 'required');
33+
$this->form_validation->set_rules('c_password', 'Confirm Password', 'required|matches[password]');
34+
}
35+
36+
if($this->form_validation->run() == FALSE){
37+
$json['errors'] = $this->form_validation->error_array();
38+
}
39+
40+
if(!isset($json['errors']['email'])){
41+
$emailCheck = User::whereNotIn("id",[$user_id])->where("email","like",$data['email'])->count();
42+
if($emailCheck > 0){
43+
$json['errors']['email'] = "Email Address is already exist";
44+
}
45+
}
46+
47+
if (!isset($json['errors'])) {
48+
$user = User::findOrNew($user_id);
49+
$user->name = $data['name'];
50+
$user->email = $data['email'];
51+
$user->status = (int)$data['status'];
52+
53+
if(trim($data['password']) != '') {
54+
$user->password = bcrypt_hash($data['password']);
55+
}
56+
57+
$user->save();
58+
$json['redirect'] = route("admin.user.list");
59+
}
60+
61+
62+
View::json($json);
63+
}
64+
65+
public function destory($user_id){
66+
User::where("id",$user_id)->delete();
67+
redirect(route('admin.user.list'));
68+
}
69+
70+
public function destory_multiple(){
71+
$ids = $this->input->post('ids');
72+
73+
if(is_array($ids) && $ids){
74+
User::whereIn("id",$ids)->delete();
75+
}
76+
redirect(route('admin.user.list'));
77+
}
78+
}

application/core/Auth.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
use Model\User;
3+
4+
class Auth{
5+
public static $user = false;
6+
function __construct(){}
7+
8+
private static function initClass(){
9+
if(!self::$user){
10+
$ci = &get_instance();
11+
$id = (int)$ci->session->userdata('login_admin');
12+
self::$user = User::find($id);
13+
}
14+
}
15+
16+
public static function user(){
17+
self::initClass();
18+
return self::$user;
19+
}
20+
21+
public static function check(){
22+
self::initClass();
23+
return self::$user->id > 0;
24+
}
25+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
function dateFormat($date){
4+
return date("d M Y h:i A",strtotime($date));
5+
}
6+
7+
function makePaginate(&$query, $config){
8+
$limit = $query->perPage();
9+
$total = $query->total();
10+
$page = $query->currentPage();
11+
$first = $total > 0 ? (($page * $limit) - $limit +1) : 0;
12+
$last = ($total > ($page * $limit)) ? ($page * $limit) : $total;
13+
14+
$query->paginate_text = "Showing <span>". ($first) ."</span> to <span>". $last ."</span> of <span>". $total ."</span> entries";
15+
16+
$config['use_page_numbers'] = TRUE;
17+
$config['reuse_query_string'] = TRUE;
18+
$config['total_rows'] = $total;
19+
$config['per_page'] = $limit;
20+
$config['full_tag_open'] = '<ul class="pagination m-0 ml-auto">';
21+
$config['full_tag_close'] = '</ul>';
22+
$config['num_tag_open'] = '<li class="page-item">';
23+
$config['num_tag_close'] = '</li>';
24+
$config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';
25+
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';
26+
$config['next_tag_open'] = '<li class="page-item">';
27+
$config['next_tagl_close'] = '<span aria-hidden="true">&raquo;</span></li>';
28+
$config['prev_tag_open'] = '<li class="page-item">';
29+
$config['prev_tagl_close'] = '</li>';
30+
$config['first_tag_open'] = '<li class="page-item">';
31+
$config['first_tagl_close'] = '</li>';
32+
$config['last_tag_open'] = '<li class="page-item">';
33+
$config['last_tagl_close'] = '</li>';
34+
$config['next_link'] = 'Next <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon"><polyline points="9 18 15 12 9 6"></polyline></svg>';
35+
$config['prev_link'] = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon"><polyline points="15 18 9 12 15 6"></polyline></svg> Prev';
36+
$config['attributes'] = array('class' => 'page-link');
37+
38+
$ci = &get_instance();
39+
$ci->pagination->initialize($config);
40+
$query->paginate = $ci->pagination->create_links();
41+
}
42+
43+
function bcrypt_hash($password, $work_factor = 8){
44+
if (! function_exists('openssl_random_pseudo_bytes')) {
45+
throw new Exception('Bcrypt requires openssl PHP extension');
46+
}
47+
48+
if ($work_factor < 4 || $work_factor > 31) $work_factor = 8;
49+
$salt =
50+
'$2a$' . str_pad($work_factor, 2, '0', STR_PAD_LEFT) . '$' .
51+
substr(
52+
strtr(base64_encode(openssl_random_pseudo_bytes(16)), '+', '.'),
53+
0, 22
54+
)
55+
;
56+
57+
return crypt($password, $salt);
58+
}
59+
60+
function bcrypt_check($password, $stored_hash, $legacy_handler = NULL){
61+
if (bcrypt_is_legacy_hash($stored_hash)) {
62+
if ($legacy_handler) return call_user_func($legacy_handler, $password, $stored_hash);
63+
else throw new Exception('Unsupported hash format');
64+
}
65+
66+
return crypt($password, $stored_hash) == $stored_hash;
67+
}
68+
69+
function bcrypt_is_legacy_hash($hash) { return substr($hash, 0, 4) != '$2a$'; }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
class View{
3+
function __construct(){}
4+
5+
public static function load($file, $data = array()){
6+
$ci = &get_instance();
7+
$data['content'] = $ci->load->view($file, $data, true);
8+
9+
$ci->load->view('admin/layout/dashboard', $data);
10+
}
11+
12+
public static function json($json = array()){
13+
echo json_encode($json);die;
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
use Luthier\MiddlewareInterface;
3+
4+
class CheckIfLogin implements MiddlewareInterface {
5+
public function run($args){
6+
$ci = &get_instance();
7+
$user_id = (int)$ci->session->userdata('login_admin');
8+
9+
if($user_id <= 0){
10+
$ci->session->unset_userdata('login_admin');
11+
redirect(route('admin.login'));
12+
}
13+
}
14+
}

application/models/User.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
use \Illuminate\Database\Eloquent\Model as Eloquent;
44

55
class User extends Eloquent {
6-
6+
public function getStatusTextAttribute(){
7+
switch ($this->status) {
8+
case 0: return "<span class='badge bg-danger'>Disabled</spam>"; break;
9+
case 1: return "<span class='badge bg-gray'>Active</spam>"; break;
10+
default: return $this->status; break;
11+
}
12+
}
713
}

0 commit comments

Comments
 (0)