Advanced control Auth::Check - laravel

if the columns in the table value of admin, I want to show the admin page, not water to another page
$type = array('type' =>'Admin');
if (Auth::check($type))
{
return view('admin.index');
}
Auth::logout();
return View::make('admin.auth.login');

You'll have to change your RedirectIfAuthenticated.php file. It's stored in /app/Http/Middleware. In the handle function you can check if the user has a specific value in the database and redirect to another page accordingly.

I solved the problem
public function showLogin()
{
if (Auth::check())
{
if (auth()->user()->type!='Admin')
{
return view('helperpage.admin-yetki');
}
else
{
return view('admin.index');
}
}
return View::make('admin.auth.login');
}

Related

How to make the profile user forbid to anyone except the own user?

I have route:
Route::get('#{username}', 'HomePageController#username')->name('user.profile');
that route to allow for everyone to see the profile ( contains his info and his cv .. etc ), and in the beginning of register any user user must wait to active his account by the admin
I need to see if account of user still under process show above route just for him. and when the account is active open above route for everyone can see his profile.
I tried to create middleware but don't know how can I forbid the guest user
My wrong shut:
public function username($username)
{
$user = User::where('username' , '=' , $username)->firstOrFail();
if($user->active){
return view('frontend.user_profile',compact('user','projects_last','first_project','whole_projects'));
}else{
return redirect('/');
}
}
What the best scenario to do something like that?
thanks.
if ($user->active) {
// Everyone can see
} else {
if (Auth::user() && Auth::user()->username == $username) {
// only auth and himself can see
} else {
// redirect to home page
}
}
if($user->active || $username == Auth::user()->username){
return view('frontend.user_profile',compact('user','projects_last','first_project','whole_projects'));
}else{
return redirect('/');
}
You can try this
public function username($username)
{
$user = User::where(['username' => $username,'status' => 'active'])->firstOrFail();
if($user && auth()->user()->username == $username){
return view('frontend.user_profile',compact('user','projects_last','first_project','whole_projects'));
}else{
return abort(403, 'Unauthorized action.');
}
}
}
You can also use it like this
if( $username == Auth::user()->username){
return view('frontend.user_profile',compact('user','projects_last','firs. t_project','whole_projects'));
}else{
return abort(403, 'Unauthorized action.');
}

How to pass data from return()->redirect()->back()?

I want to add data to the database selecting from the dropdown.
After adding the data it redirects me to the same page.
i want select dropdown to select the same data that i selected before inserting data in database.
I didn't allow me by passing value from return redirect back.
Are there any alternatives?
public function district()
{
$data['districts'] = District::all();
$data['locations'] = Location::paginate(6);
return view('Backend.pages.dashboard.district',$data);
}
public function addingdistrict(Request $request)
{
if($request->isMethod('get'))
{
return redirect()->back()->with('danger','Not with URL please');
}
if($request->isMethod('post'))
{
$data['district_id'] = $request->districtname;
$data['location'] = $request->locationname;
if(Location::create($data))
{
return redirect()->back();
}
}
}
i want to pass data from addingdistrict return redirect()->back()
redirect()->back()->with('message','your message')->with(compact('your_varible'));

How to restrict user to add input duplicate data into database using codeigniter

I'd One text_box(txtname) & Add button in View . I've to restrict user to add already existing(duplicate) data in a database . after clicking on Add button. what changes should I've to do in Model & controller.
I tried to copy+ paste my model & controller code but due to some restrictions of website i'm not getting able to display code. please kindly suggest me what should I've to do ?
try this i hope it will help you.
first of all get data by textAddItem
//controller save data function
//get txtAdditem
$txtAddItem = $this->modelname->method_name($this->input->post('textAddItem'));
if (count($txtAddItem) > 0) {
//already existed
}
else{
$data=array(
$name=>$this->input->post('textAddItem')
);
return $query=$this->db->insert('tbl_category',$data);
}
//modele
function method_name($textAddItem) {
return $this->db->where('cat_name', $textAddItem)
->get('tbl_category')->row_array();
}
First, check the cat_name in the DB before inserting in the controller
Controller
public function function_name(){
$this->load->model('CrudModel');
$data = array('cat_name' => $name);
$if_exists = $this->CrudModel->check_category($data);
if($if_exists > 0){
//Already Exists
}else{
//New insertion
}
}
Model
public function check_category($data){
return $this->db->get_where('tbl_cateogry', $data)->num_rows();
}
public function insertMenurecord()
{
$this->load->model('CrudModel');
$this->form_validation->set_rules('txtMenuItem', 'Menu','required|is_unique[tbl_menumaster.menu_name]');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('msg','&nbsp&nbsp&nbsp Inserted record already exist');
redirect('Welcome/displayMasterMenu');
}
else
{
$this->CrudModel->saveMenudata();
$this->session->set_flashdata('msg','You have successfully Inserted record');
redirect('Welcome/displayMasterMenu');
}
}

Laravel Auth Login Solution

I create postSignIn Method and want to verified :
email, password, verifiedFlag
First there was no problem for create postSignIn Method, like :
public function postSignIn(){
if(Auth::attempt(array('email' => Input::get('email'),'password' => Input::get('password'),'verifiedFlag'=>1))){
return Redirect::route('home-view');
}
else{
return "Email/Password wrong or Your Account not verified by Admin";
}
}
But now I try to make it more user friendly by Separate Alert for
Account not Verified, and
Email/Password Wrong
and now I try to make it like this:
if(Auth::attempt(array('nim' => Input::get('nim'),'password' => Input::get('password')))){
Auth::logout();
if(Auth::attempt(array('nim' => Input::get('nim'),'password' => Input::get('password'),'verified' => 1))){
return Redirect::route('home-view');
}
else{
return "Your Account not verfied. Please wait until admin verified your account or contact your admin";
}
}
else{
return "NIM/Password wrong";
}
there was no problem, but I think I need other solution so Auth don't need to Login(Attempt) Twice
You can use the validate method. This would work:
public function postSignIn(){
if(Auth::attempt(array('email' => Input::get('email'),'password' => Input::get('password'),'verifiedFlag'=>1))){
return Redirect::route('home-view');
}
elseif(Auth::validate(array('email' => Input::get('email'),'password' => Input::get('password')))){
return "Your Account not verified by Admin";
}
else
{
return "Email/Password wrong";
}
}
Filters are the way to go. It's easy and clean to solve this problem, see my example below.
if user is inactive at any point it will logout user,
you can redirect user with Session flash message, your login code works as it is.
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('login');
}
}
else
{
// If the user is not active any more, immidiately log out.
if(Auth::check() && !Auth::user()->verifiedFlag)
{
Auth::logout();
Session::flash('message','Your account is not active, please contact your administrator to active your account');
// redirect to login page
return Redirect::to('/');
}
}
});

How to redirect index page if user not logged in laravel

Hello i create website in laravel but i facing one problem. The problem is that when user is not log in and user type www.test.com/notifications that time showing error like this
ErrorException (E_UNKNOWN)
Undefined variable: messages (View: /home/test/app/views/message-page.blade.php)
But i want to when user is not log in and enter www.test.com/notifications so user automatic redirect to index page. Please help me i very confuse.
I using the some code in base controller is as follows:
public function checkLoggedIn(){
if(Auth::user()->check()){
return;
}
else {
return Redirect::to("/");
}
}
You should do it this way:
public function checkLoggedIn(){
if (!Auth::check()) {
return Redirect::to("/");
}
return true;
}
However I assume you want to use this function in another controller so then you should do it this way:
$result = $this->checkLoggedIn();
if ($result !== true) {
return $result;
}
to make redirection.
But Laravel have filters so you can easily check if user is logged.
You can just use in your routes.php:
Route::group(
['before' => 'auth'],
function () {
// here you put all paths that requires user authentication
}
);
And you can adjust your filter in app/filters for example:
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::to('/');
}
}
});

Resources