-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
defined('BASEPATH') OR exit('No direct script access allowed'); | ||
|
||
class Auth extends MY_Controller { | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->load->helper('auth'); | ||
} | ||
|
||
public function test_login($role) | ||
{ | ||
/** | ||
* Make userdata object as you want | ||
* For this example, we need a role that will used | ||
* for role middleware | ||
*/ | ||
$userdata = (object) [ | ||
'id' => 1, | ||
'name' => 'Nur Muhammad', | ||
'role' => $role | ||
]; | ||
|
||
set_userdata($userdata); | ||
|
||
return send_response([ | ||
'success' => TRUE, | ||
'data' => $userdata | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
define('AUTH_SESS_NAME', 'app_logged_in'); | ||
|
||
function set_userdata($data) { | ||
$_ci =& get_instance(); | ||
$_ci->session->set_userdata(AUTH_SESS_NAME, $data); | ||
} | ||
|
||
function userdata() { | ||
$_ci =& get_instance(); | ||
|
||
$userdata = $_ci->session->userdata(AUTH_SESS_NAME); | ||
|
||
return $userdata; | ||
} | ||
|
||
function clear_userdata() { | ||
$_ci =& get_instance(); | ||
$_ci->session->unset_userdata(AUTH_SESS_NAME); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
class Auth_middleware { | ||
|
||
private $ci; | ||
private $controller; | ||
private $extras; | ||
|
||
/** | ||
* Accepting codeigniter instance, current controller & extras | ||
*/ | ||
public function __construct($ci, $controller, ...$extras) | ||
{ | ||
$this->ci = $ci; | ||
$this->controller = $controller; | ||
$this->extras = $extras; | ||
} | ||
|
||
public function run() | ||
{ | ||
$this->ci->load->helper([ | ||
'api', | ||
'auth' | ||
]); | ||
|
||
if (empty(userdata())) { | ||
return send_response([ | ||
'success' => FALSE, | ||
'error' => 'Login required!' | ||
], HTTP_UNAUTHORIZED); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
class Role_middleware { | ||
|
||
private $ci; | ||
private $controller; | ||
private $extras; | ||
|
||
/** | ||
* Accepting codeigniter instance & controller | ||
*/ | ||
public function __construct($ci, $controller, $extras) | ||
{ | ||
$this->ci = $ci; | ||
$this->controller = $controller; | ||
$this->extras = $extras; | ||
} | ||
|
||
public function run() | ||
{ | ||
$this->ci->load->helper([ | ||
'api', | ||
'auth' | ||
]); | ||
|
||
$allowed_roles = $this->extras['roles']; | ||
|
||
$userdata = userdata(); | ||
$role = $userdata->role ?? NULL; | ||
|
||
if (!in_array($role, $allowed_roles)) { | ||
return send_response([ | ||
'success' => FALSE, | ||
'error' => 'Sorry, you don\'t have access to this resource.' | ||
], HTTP_FORBIDDEN); | ||
} | ||
} | ||
} |