How to differentiate the multiple panels with login and session? - codeigniter

It create the session but does not go to index2 and index3 always redirect with else and go to index method but i want to go index2 and index3 to handle other panels also.
Session is created successfully for all just comming else condition all the time.
My form data and array is also showing when i using the print_r for my code to view if the data is comming or not.
Problem is it is showing no any error just redirect with file of index method.
My Controller
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Main_Model');
$this->load->helper('url');
$this->load->library('session');
$method = $this->router->fetch_method();
$methods = array('index','index2','index3');
if(in_array($method,$methods))
{
if(!$this->session->has_userdata('signup_email'))
{
redirect(base_url('Main/login'));
}
}
}
public function index()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('BKO/index');
}
}
public function index2()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Admin/index');
}
}
public function index3()
{
if($this->session->has_userdata('signup_email'))
{
$this->load->view('Owner/index');
}
}
public function login()
{
//$data['select'] = $this->Main_Model->get_select();
$this->load->view('login');
}
public function login_process()
{
//$roll = $this->input->post('select');
echo $email = $this->input->post('email');
echo $pass = $this->input->post('upass');
$query = $this->Main_Model->login_process($email,$pass);
if($query == TRUE)
{
$this->session->set_userdata('signup_email');
$session = array(
'signup_email' => $email
);
$this->session->set_userdata($session);
redirect(base_url('Main/check_login'));
}
else
{
$this->session->set_flashdata('error','Invalid Email or Password');
redirect(base_url('Main/login'));
}
}
public function check_login()
{
if($this->session->userdata() == 'admin#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index2'));
}
elseif($this->session->userdata() == 'owner#gmail.com')
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index3'));
}
else
{
echo "Welcome - <h2>".$this->session->userdata('username')."</h2>";
redirect(base_url('Main/index'));
}
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url());
}
My Model
public function login_process($email,$pass)
{
//$this->db->select('*');
//$this->db->where('roll_id',$roll);
$this->db->where('signup_email',$email);
$this->db->where('signup_password',$pass);
$query = $this->db->get('signup');
if($query->num_rows() > 0)
{
$this->session->set_flashdata('signup_email');
return true;
}
else
{
return false;
}
}

You missed the parameter here
if($this->session->userdata() == 'admin#gmail.com')
instead it should be
if($this->session->userdata('signup_email') == 'admin#gmail.com')

Related

Shows an error when using the Gate facade

When writing privileges and rights of a user shows error: 403
Forbidden
Controller code
class IndexController extends AdminController
{
public function __construct(){
parent::__construct();
if (Gate::denies('VIEW_ADMIN')) {
abort(403);
}
$this->template = env('THEME').'.admin.index';
}
AuthServiceProvider code
public function boot()
{
$this->registerPolicies();
Gate::define('VIEW_ADMIN', function($user){
return $user->canDo('VIEW_ADMIN');
});
//
}
Model User code
The User model is associated with the Roles model, and the Roles model is associated with the Permission model.
public function canDo($permission, $require = FALSE){
if (is_array($permission)) {
dump($permission);
}
else{
foreach ($this->roles as $role) {
foreach ($this->permissions as $permission) {
if (str_is($permission,$permission->name)) {
return true;
}
}
}
}
}
You rewrite input $permission on line foreach ($this->permissions as $permission) { so your if (str_is($permission,$permission->name)) is always FALSE because
str_is(array(), 'VIEW_ADMIN') === FALSE
You should do this
public function canDo($permission, $require = FALSE){
if (is_array($permission)) {
dump($permission);
}
else{
foreach ($this->roles as $role) {
foreach ($this->permissions as $permissionObject) {
if (str_is($permission,$permissionObject->name)) {
return true;
}
}
}
}
}
Also you should add return FALSE because return type is boolean in this case.

Laravel Redirect to previous dynamic page after login

I wonder how I can redirect a user after login?
Lets say that I am on the page "www.mysite.com/users/2"
Then I try to edit a blog post without being logged in and get sent to the login page, efter login I wish to return to "www.mysite.com/users/2"
I have tried this so far:
if (Auth::attempt($credentials,$remember)) {
return redirect()->back();
} else {
return redirect()->back()->withErrors([trans('api.couldnotlogin')]);
}
But return redirect()->back(); will only redirect me to "www.mysite.com/"
Update
I got it working using this:
public function showLoginForm()
{
$previous_url = Session::get('_previous.url');
$ref = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$ref = rtrim($ref, '/');
if ($previous_url != url('login')) {
Session::put('referrer', $ref);
if ($previous_url == $ref) {
Session::put('url.intended', $ref);
}
}
return view('auth.login');
}
public function loginUser(ApiAuthUserPassRequest $request)
{
if ($request->has('rememberme')) {
$remember = $request->input('rememberme');
} else {
$remember = false;
}
$credentials = ['email' => $request->input('email'), 'password' => $request->input('password')];
if (Auth::attempt($credentials,$remember)) {
if (Session::has('referrer')) {
return redirect()->intended(Session::pull('referrer'));
} else {
return redirect('/account');
}
} else {
return redirect()->back()->withErrors([trans('api.couldnotlogin')]);
}
}
Laravel 5.1 have trait Illuminate/Foundation/Validation/ValidatesRequests.php with method
protected function getRedirectUrl()
{
return app(UrlGenerator::class)->previous();
}
where UrlGenerator is Illuminate/Routing/UrlGenerator.php. You can try use previous() method.

Controller method's result is not accessible on view in CodeIgniter

I am trying to print a controller method's result on view but it is giving me an error:
Undefined variable: $states. Can someone help me to point out what is wrong in my code?
Model code:
public function state_names() {
$query = $this->db->select('name')
->get('place')
->where('parent','India');
$query->db->get();
return $query->result();
}
Controller Code:
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states']) && $st['states']->num_rows() > 0) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}
View code:
<?php foreach ($states as $state) {
echo $state->name;
}
try this one
public function state_names() {
$this->db->select('name')
$this->db->get('place')
$this->db->where('parent','India');
$query=$this->db->get();
return $query->result_array();
}
In your model you are running query twice - for each method get(). You should run it once:
public function state_names() {
$query = $this->db->select('name')
->where('parent','India')
->get('place');
return $query->result();
}
In your controller you can't check num_rows() because there are results - not full response from database.
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states'])) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}
Your Problem is 2 place First in Model and second in controller
if (isset($st['states']) && $st['states']->num_rows() > 0)
and Model
Model Solution
public function state_names() {
$query = $this->db->select('name')
->where('parent','India')
->get('place');
// $query->db->get();
if($query->num_rows() > 0){
return $query->result();
}
}
N.B [ Here Where will be first and get will be last its good practice ];
Controller Solution :
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states']) ) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}`

Check if has beign redirected from another controller

On my login controller or view I would like to be able to see if it is possible to check if page has redirected back to login and then display bootstrap error message. I also use a MY_controller function in codeigniter
I do not want to use codeigniter session flash data message. I have my own error message as showing in code.
Is it possible to check if controller has been redirect from another controller?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Administration';
$user_id = $this->session->userdata('user_id');
if (isset($user_id)) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login.tpl', $data);
} else {
redirect('admin/dashboard');
}
}
public function validate() {
$this->load->library('user');
if ($this->user->login() == FALSE) {
$this->form_validation->set_message('validate', 'Does not match any of our database records');
return false;
} else {
return true;
}
}
}
MY Controller
<?php
class MY_Controller extends MX_Controller {
public function __construct() {
parent::__construct();
Modules::run('admin/error/permission/check');
}
}
Update I have tried This still displays message even though have not been redirect from another page.
Thanks to #AdrienXL for some great advice. $this->load->library('user_agent'); was the best method.
if ($this->agent->referrer()) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Administration';
$this->load->library('user_agent');
if ($this->agent->referrer()) {
$this->error['warning'] = "Working";
} else {
$this->error['warning'] = "";
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$this->form_validation->set_rules('username', 'Username', 'required|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login.tpl', $data);
} else {
redirect('admin/dashboard');
}
}
public function validate() {
$this->load->library('user');
if ($this->user->login() == FALSE) {
$this->form_validation->set_message('validate', 'Does not match any of our database records');
return false;
} else {
return true;
}
}
}

Query Not Loading In Libaray File In CI

I am trying to convert my model user to a library file and place it in the system libraries. because I have multiple installs of codeigniter.
On my line 16 which is just below my public function login, its saying Call to a member function query() on a non-object in
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User {
private $user_id;
private $username;
public function __construct() {
$CI =& get_instance();
$CI->load->library('session');
$CI->load->library('bcrypt');
$CI->load->database();
}
public function login() {
// Line 16 $user_query = $this->db->query("SELECT * FROM " . $this->input->get('db_prefix') . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = '" . $this->db->escape($password) . "') AND status = '1'");
$result = $this->check_credentials($password);
$this->user_id = $user_query->row['user_id'];
$this->username = $user_query->row['username'];
$user_query = $this->db->get('user');
if($user_query->num_rows == 1) {
return true;
} else {
return false;
}
}
public function check_credentials() {
$user_query = $this->db->get('user');
if ($user_query->num_rows == 1) {
$result = $query->row_array();
if ($this->bcrypt->check_password($password, $result['password'])) {
//We're good
return $result;
} else {
//Wrong password
return array();
}
} else {
return array();
}
}
public function isLogged() {
return $this->user_id;
}
public function getId() {
return $this->user_id;
}
public function getUserName() {
return $this->username;
}
}
After having a break and thinking about it I did re try with what #Mehul Jethloja said
And got it working
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User {
private $user_id;
private $username;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->library('bcrypt');
$this->CI->load->database();
}
public function login($username, $password) {
$this->CI->db->where('username', $this->CI->input->post('username'), $username);
$this->CI->db->where('password', $this->CI->input->post('password'), $password);
$result = $this->check_credentials($password);
$this->user_id = $user_query->row['user_id'];
$this->username = $user_query->row['username'];
$user_query = $this->CI->db->get('user');
if($user_query->num_rows == 1) {
return true;
} else {
return false;
}
}
public function check_credentials() {
$user_query = $this->CI->db->get('user');
if ($user_query->num_rows == 1) {
$result = $query->row_array();
if ($this->bcrypt->check_password($password, $result['password'])) {
//We're good
return $result;
} else {
//Wrong password
return array();
}
} else {
return array();
}
}
public function isLogged() {
return $this->user_id;
}
public function getId() {
return $this->user_id;
}
public function getUserName() {
return $this->username;
}
}

Resources