Skip to content

Commit e027603

Browse files
committed
login history implemented
1 parent 16f3083 commit e027603

File tree

7 files changed

+174
-9
lines changed

7 files changed

+174
-9
lines changed

application/config/routes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
$route['changePassword'] = "user/changePassword";
6161
$route['pageNotFound'] = "user/pageNotFound";
6262
$route['checkEmailExists'] = "user/checkEmailExists";
63+
$route['login-history'] = "user/loginHistoy";
64+
$route['login-history/(:num)'] = "user/loginHistoy/$1";
65+
$route['login-history/(:num)/(:num)'] = "user/loginHistoy/$1/$2";
6366

6467
$route['forgotPassword'] = "login/forgotPassword";
6568
$route['resetPasswordUser'] = "login/resetPasswordUser";

application/controllers/user.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ function userListing()
4141
$this->loadThis();
4242
}
4343
else
44-
{
45-
$this->load->model('user_model');
46-
44+
{
4745
$searchText = $this->input->post('searchText');
4846
$data['searchText'] = $searchText;
4947

@@ -328,6 +326,40 @@ function pageNotFound()
328326

329327
$this->loadViews("404", $this->global, NULL, NULL);
330328
}
329+
330+
/**
331+
* This function used to show login history
332+
* @param number $userId : This is user id
333+
*/
334+
function loginHistoy($userId = NULL)
335+
{
336+
if($this->isAdmin() == TRUE)
337+
{
338+
$userId = $this->session->userdata("userId");
339+
}
340+
else
341+
{
342+
$userId = ($userId == NULL ? $this->session->userdata("userId") : $userId);
343+
}
344+
345+
$searchText = $this->input->post('searchText');
346+
347+
$data["userInfo"] = $this->user_model->getUserInfoById($userId);
348+
349+
$data['searchText'] = $searchText;
350+
351+
$this->load->library('pagination');
352+
353+
$count = $this->user_model->loginHistoryCount($userId);
354+
355+
$returns = $this->paginationCompress ( "login-history/".$userId."/", $count, 5, 3);
356+
357+
$data['userRecords'] = $this->user_model->loginHistory($userId, $returns["page"], $returns["segment"]);
358+
359+
$this->global['pageTitle'] = 'CodeInsect : User Login History';
360+
361+
$this->loadViews("loginHistory", $this->global, $data, NULL);
362+
}
331363
}
332364

333365
?>

application/libraries/BaseController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ function loadViews($viewName = "", $headerInfo = NULL, $pageInfo = NULL, $footer
111111
* @param {number} $perPage : This is records per page limit
112112
* @return {mixed} $result : This is array of records and pagination data
113113
*/
114-
function paginationCompress($link, $count, $perPage = 10) {
114+
function paginationCompress($link, $count, $perPage = 10, $segment = SEGMENT) {
115115
$this->load->library ( 'pagination' );
116-
116+
117117
$config ['base_url'] = base_url () . $link;
118118
$config ['total_rows'] = $count;
119-
$config ['uri_segment'] = SEGMENT;
119+
$config ['uri_segment'] = $segment;
120120
$config ['per_page'] = $perPage;
121121
$config ['num_links'] = 5;
122122
$config ['full_tag_open'] = '<nav><ul class="pagination">';
@@ -140,7 +140,7 @@ function paginationCompress($link, $count, $perPage = 10) {
140140

141141
$this->pagination->initialize ( $config );
142142
$page = $config ['per_page'];
143-
$segment = $this->uri->segment ( SEGMENT );
143+
$segment = $this->uri->segment ( $segment );
144144

145145
return array (
146146
"page" => $page,

application/models/user_model.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,58 @@ function changePassword($userId, $userInfo)
187187

188188
return $this->db->affected_rows();
189189
}
190+
191+
192+
/**
193+
* This function is used to get user login history
194+
* @param number $userId : This is user id
195+
*/
196+
function loginHistoryCount($userId)
197+
{
198+
$this->db->select('BaseTbl.userId, BaseTbl.sessionData, BaseTbl.machineIp, BaseTbl.userAgent, BaseTbl.agentString, BaseTbl.platform, BaseTbl.createdDtm');
199+
$this->db->where('BaseTbl.userId', $userId);
200+
$this->db->from('tbl_last_login as BaseTbl');
201+
$query = $this->db->get();
202+
203+
return $query->num_rows();
204+
}
205+
206+
/**
207+
* This function is used to get user login history
208+
* @param number $userId : This is user id
209+
* @param number $page : This is pagination offset
210+
* @param number $segment : This is pagination limit
211+
* @return array $result : This is result
212+
*/
213+
function loginHistory($userId, $page, $segment)
214+
{
215+
$this->db->select('BaseTbl.userId, BaseTbl.sessionData, BaseTbl.machineIp, BaseTbl.userAgent, BaseTbl.agentString, BaseTbl.platform, BaseTbl.createdDtm');
216+
$this->db->from('tbl_last_login as BaseTbl');
217+
$this->db->where('BaseTbl.userId', $userId);
218+
$this->db->order_by('BaseTbl.id', 'DESC');
219+
$this->db->limit($page, $segment);
220+
$query = $this->db->get();
221+
222+
$result = $query->result();
223+
return $result;
224+
}
225+
226+
/**
227+
* This function used to get user information by id
228+
* @param number $userId : This is user id
229+
* @return array $result : This is user information
230+
*/
231+
function getUserInfoById($userId)
232+
{
233+
$this->db->select('userId, name, email, mobile, roleId');
234+
$this->db->from('tbl_users');
235+
$this->db->where('isDeleted', 0);
236+
$this->db->where('userId', $userId);
237+
$query = $this->db->get();
238+
239+
return $query->row();
240+
}
241+
190242
}
191243

192244

application/views/includes/header.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
</a>
5454
<div class="navbar-custom-menu">
5555
<ul class="nav navbar-nav">
56+
<li>
57+
<a href="<?= base_url() ?>login-history"><i class="fa fa-history"></i></a>
58+
</li>
5659
<!-- User Account: style can be found in dropdown.less -->
5760
<li class="dropdown user user-menu">
5861
<a href="#" class="dropdown-toggle" data-toggle="dropdown">

application/views/loginHistory.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<div class="content-wrapper">
2+
<!-- Content Header (Page header) -->
3+
<section class="content-header">
4+
<h1>
5+
<i class="fa fa-users"></i> Login History
6+
<small>Add, Edit, Delete</small>
7+
</h1>
8+
</section>
9+
<section class="content">
10+
<div class="row">
11+
<div class="col-xs-12">
12+
<div class="box">
13+
<div class="box-header">
14+
<h3 class="box-title"><?= $userInfo->name." : ".$userInfo->email ?></h3>
15+
<div class="box-tools">
16+
<form action="<?php echo base_url() ?>login-history" method="POST" id="searchList">
17+
<div class="input-group">
18+
<input type="text" name="searchText" value="<?php echo $searchText; ?>" class="form-control input-sm pull-right" style="width: 150px;" placeholder="Search"/>
19+
<div class="input-group-btn">
20+
<button class="btn btn-sm btn-default searchList"><i class="fa fa-search"></i></button>
21+
</div>
22+
</div>
23+
</form>
24+
</div>
25+
</div><!-- /.box-header -->
26+
<div class="box-body table-responsive no-padding">
27+
<table class="table table-hover">
28+
<tr>
29+
<th>Session Data</th>
30+
<th>IP Address</th>
31+
<th>User Agent</th>
32+
<th>Agent Full String</th>
33+
<th>Platform</th>
34+
<th>Date-Time</th>
35+
</tr>
36+
<?php
37+
if(!empty($userRecords))
38+
{
39+
foreach($userRecords as $record)
40+
{
41+
?>
42+
<tr>
43+
<td><?php echo $record->sessionData ?></td>
44+
<td><?php echo $record->machineIp ?></td>
45+
<td><?php echo $record->userAgent ?></td>
46+
<td><?php echo $record->agentString ?></td>
47+
<td><?php echo $record->platform ?></td>
48+
<td><?php echo $record->createdDtm ?></td>
49+
</tr>
50+
<?php
51+
}
52+
}
53+
?>
54+
</table>
55+
56+
</div><!-- /.box-body -->
57+
<div class="box-footer clearfix">
58+
<?php echo $this->pagination->create_links(); ?>
59+
</div>
60+
</div><!-- /.box -->
61+
</div>
62+
</div>
63+
</section>
64+
</div>
65+
<script type="text/javascript">
66+
jQuery(document).ready(function(){
67+
jQuery('ul.pagination li a').click(function (e) {
68+
e.preventDefault();
69+
var link = jQuery(this).get(0).href;
70+
jQuery("#searchList").attr("action", link);
71+
jQuery("#searchList").submit();
72+
});
73+
});
74+
</script>

application/views/users.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
<td><?php echo $record->mobile ?></td>
5454
<td><?php echo $record->role ?></td>
5555
<td class="text-center">
56-
<a class="btn btn-sm btn-info" href="<?php echo base_url().'editOld/'.$record->userId; ?>"><i class="fa fa-pencil"></i></a>
57-
<a class="btn btn-sm btn-danger deleteUser" href="#" data-userid="<?php echo $record->userId; ?>"><i class="fa fa-trash"></i></a>
56+
<a class="btn btn-sm btn-primary" href="<?= base_url().'login-history/'.$record->userId; ?>" title="Login history"><i class="fa fa-history"></i></a> |
57+
<a class="btn btn-sm btn-info" href="<?php echo base_url().'editOld/'.$record->userId; ?>" title="Edit"><i class="fa fa-pencil"></i></a>
58+
<a class="btn btn-sm btn-danger deleteUser" href="#" data-userid="<?php echo $record->userId; ?>" title="Delete"><i class="fa fa-trash"></i></a>
5859
</td>
5960
</tr>
6061
<?php

0 commit comments

Comments
 (0)