I'm trying to create a check for the field member is equal to 1.
I've created a helper class and added it in my app.blade.php
Here is my code.
Helper Class:
function checkMember() {
$stmt = DB::table('users')->where('member', '=', 1);
if ($stmt) {
return True;
}
return False;
}
app.blade.php
#if (checkMember() == True)
<li><i class="fa fa-btn fa-terminal"></i>Member Panel</li>
#endif
The error is that it displays the <li> even when member == 0 in the DB
Your checkMember() method is incomplete - it doesn't launch the query, it only builds it, so it will always be true. Try this:
function checkMember() {
$stmt = DB::table('users')->where('member', 1)->first();
return $stmt ? true : false;
}
I'm also concerned about your query a little bit - this will check if there is any user with 'member' field set to true. So, if you have at least one member - it will return true for all your web users.
Don't you want to do this check against current, logged in user?
Related
I have two functions, one to check whether a user is logged in, and another to check if the user is an admin. I also have a User database with one column named user_lvl, which displays fine if I output all the user data.
The problem I'm having is that with the admin function it doesn't seem read anything.
Here is the two functions code:
define('USER_LEVEL_ADMIN', '1');
// check whether a user session is active, return true if yes, else return no
function isLoggedIn() {
if (isset($_SESSION['userId'])) {
return true;
}
else {
return false;
}
}
// check whether user has required user level to access admin privileges, return true if yes
function isAdmin() {
// check if a user is in a session, then check if the users user_lvl is equal to 'USER_LEVEL_ADMIN
if (isset($_SESSION['userId']) && $_SESSION['userId'] && USER_LEVEL_ADMIN == $_SESSION['userId']['user_Lvl']) {
return true;
}
else { // works if you reverse true and false, else this is broke
return false;
}
}
And here is where it is being called:
<?php if (isLoggedIn() ) : ?>
<?php if (isAdmin() ) : ?>
<div>
Admin Panel
</div>
<?php endif; ?>
<div>
My Account
</div>
<?php endif; ?>
It displays 'My Account' but not 'Admin Panel'. Any help is much appreciated.
This code snippet is ment for testing and identify which function is gives this output
<?php
function isLoggedIn() {
return true;
}
function isAdmin() {
return false; // change it to true to see Admin Panel. You need to check the condition in this function.
}
if (isLoggedIn() ) :
if (isAdmin() ) :
echo '
<div>
Admin Panel
</div>';
endif;
echo '
<div>
My Account
</div>';
endif;
?>
The isAdmin() condition looks fine, You may echo out the session variable and crosscheck.
One of the 3 checks in the if statement is failing (returning false):
isset($_SESSION['userId']) basic isset check
$_SESSION['userId'] not sure what we're looking for here but this needs to result in boolean true
USER_LEVEL_ADMIN == $_SESSION['userId']['user_Lvl'] authorized privilege check
All 3 need to be true for the if to succeed.
if (isset($_SESSION['userId']) && $_SESSION['userId'] && USER_LEVEL_ADMIN == $_SESSION['userId']['user_Lvl']) {
return true;
}
else { // works if you reverse true and false, else this is broke
return false;
}
I suspect the if is false due to this: define('USER_LEVEL_ADMIN', '1'); which creates the named constant USER_LEVEL_ADMIN with a STRING value of '1'. Then, in your if statement you compare it to $_SESSION['userId']['user_Lvl']. Please check the variable type of $_SESSION['userId']['user_Lvl']. You can drop this line in your code to check that:
echo gettype($_SESSION['userId']['user_Lvl']);
It of course would need to match the type of USER_LEVEL_ADMIN which is string.
I have this routing:
Route::get('/item/{item_id?}',['uses'=>'ItemController#get_item_view']);
I want to redirect to the first item if no item ID is given.
public function get_item_view($id = null)
{
if($id == null)
{
#This doesnt work
$selected_item = Item::where('item_id',Auth::user()->item_id)->first();
}
else
{
#This works
$selected_item = Item::where('id',$id)
->where('item_id',Auth::user()->item_id)
->first();
}
if($selected_item === null)
{
return redirect('/');
}
return view('auth.forms.item')->with('item',$selected_item);
}
If I introduce an ID (www.myproject.com/item/2), the page works perfectly, but if I don't, and try to get just the first of the list just to generate the page. I expect www.myproject.com/item to give me the first item.
This is the error I get:
Trying to get property of non-object (View:
/var/www/myproject/resources/views/auth/forms/item.blade.php)
and when I use dd($selected_item) on both cases I get the same type of object. I have no idea why one is working and the other one isn't.
if you don't provide item id then it'll be null,
$selected_item = Item::where('item_id',null)->first();
if you want to list first item just do
if($id == null)
{
#This doesnt work
$selected_item = Item::all()->first();
}
Make sure Auth::user()->item_id has a value. the error seems to be generating from it.
you can do dd(Auth::user()->item_id) to check it
I write a method, that which checks whether the user is an administrator in file UserController.php:
public function create(){
$chackIsAdmin = Auth::user()->permissions;
if ($chackIsAdmin === 1) {
return view('users.adduser');
} else {
return redirect('warehouse');
}
In table "users" I have column "permissions". Each user is assigned a number 1 or 0. 1 - is admin, 0 is NOT an admin.
I wrote also instruction if, which displays option "Add user" only the user administrator:
#if (Auth::user()->permissions === 1)
<li>Add new user</li>
#endif
It all works correctly, but I wonder whether in Laravel I can do it in a different way ??
Whether my way is safe?
I think the better way is to write the function on the User model.
class User extends Model
{
public function isAdmin()
{
return $this->attributes['permissions'] == 1;
}
}
Then it's very easy to use...
return Auth::user()->isAdmin() ? view('users.adduser') : redirect('warehouse');
I have ACL set up with laravel. I have the following helper:
function user($user_id = null)
{
if (!$user_id) return Auth::user();
return User::where('id', $user_id)->first();
}
I have this helper function so I don't have to type the long Auth::user().
Here's the problem. I have ACL set up so when I do something like the following in my model and the user is not logged in, I get an error.
if (user()->can('edit-blog')) {
echo 'you can edit blog!';
}
This is the error if the user is not logged in:
Call to a member function can() on null
Now, I have the following code everywhere in my site. I am using ACL in almost every view and model and controller. I don't want to have to do something like the following just to not get the error.
if (user() && user()->can('edit-blog')) {
// do stuff
}
How can I return false if there is a "null" error or an exception?
** What I have tried **
I have tried try/catch but it does not do anything:
try {
if (!$user_id) return Auth::user();
return User::where('id', $user_id)->first();
} catch (Exception $ex) {
return false;
}
Thank you for your support.
You need to check if the user is logged in before calling can
if(Auth::check() && user()->can('edit-post')){
Return true;
}else{
Return false;
}
Im using laravel 4.0 im tyring to display a layout only if a variable ==0 (just in case a user tries to navigate to the url instead of clicking through) (i know I can redirect instead of extending but this is undesirable for now)
I am trying to get the layout to only extend when the user navigates to the page manually, noajax is set to true if their is no ajax request being sent when it goes to the function, so if the user where to navigate to the url manually it will still display the page but extend the layout.
#if ($noajax==1)
#extends('layouts.master')
#endif
#section('content')
//controller
public function test($id,$model)
{
if (Request::ajax())
{
//$foreign_key and $model must be <> null
if ($id == null || $model == null) {
$this->render('../Errors/missing_arg', 'error');
return;
}
if($model=="ArtObj")
{
$partable = "art_objects";
$path='img/art-objects/';
}
$parid=$id;
$noajax=0;
$mediaimgs = Media::where('parent_id' , $id )->where('parent_table', $partable)->paginate(15);
$response = Response::Json($mediaimgs);
return View::make('/Admin/manageimage/manage_image',compact('parid','mediaimgs','model','path','noajax'));
}
else{
if($model=="ArtObj")
{
$partable = "art_objects";
$path='img/art-objects/';
}
$parid=$id;
$mediaimgs = Media::where('parent_id' , $id )->where('parent_table', $partable)->paginate(15);
$response = Response::Json($mediaimgs);
$noajax = 1;
return View::make('/Admin/manageimage/manage_image',compact('parid','mediaimgs','model','path','noajax'));
}
}
In this case you should use 2 views in controller.
In controller you should use:
if ($noajax) {
return View::make('noajax');
}
else {
return View::make('ajax');
}
In noajax view you can extend from any other view and if noajax and ajax have common code, you should put it in separate file and use #include in those both views to include common part of code.