Cannot check if user has permissions - laravel

Im using the laravel spatie/permissions bundle.
Working with that bundle i ran in to a problem. I have made multiple roles and have used the $role->givePermissionTo('view users') method to give my role the permission to see my users.
Now when i use #can('view users') i cannot see the link.
i made it like this:
#can('view users')
<li class="nav-item">
<a class="nav-link font-weight-bold" href="{{ route('user.index') }}">
{{ __('User Management') }}
</a>
</li>
#endcan
When i check if the user has the permission it says it does. How come it doesnt show the link?

Related

How do I hide login link and show logout link in homepage navbar once user logs in and gets re-directed to homepage?

I'm working on a Spring boot based web application and the homepage contains a boostrap navbar with Login and Register link.
Once user logs in (by clicking login link from homepage), they will be re-directed to home page again (unless they visit any other link before spring security kicks in for A&A). On re-direct, I want to hide login and register links from the navbar and show logout link in their place. Becuase I want to insert the navbar in all the pages of application.
I want to know what is the best/standard way of doing this when the requirement is re-direction to same page.
One solution that I can think of is to check and obtain a user-principal object from spring security, pass it to thymeleaf template and check that if userprincipal object is present in the request attribute to the thymeleaf template, that means a user has logged in and I can then hide(not-render) login & register links and show(render) logout link. If not, show login & register links and hide logout link. I'm wondering, is this a correct way to do so? This feels like a hack to me and hence want to know if there is any standard way of doing this.
There is a Thymeleaf Spring Security extension that you can use:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
Add the namespace
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
And then you can use it:
<li sec:authorize="!isAuthenticated()" class="nav-item">
<a class="btn btn-outline-light" th:href="#{/login}">Login</a>
</li>
<li sec:authorize="isAuthenticated()" class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false" sec:authentication="name">
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="pl-3" th:href="#{/password}">Passwort ändern</a>
<div class="dropdown-divider"></div>
<a class="pl-3" th:href="#{/logout}">Logout</a>
</div>
</li>
Please find the whole documentation here:
https://github.com/thymeleaf/thymeleaf-extras-springsecurity

Spring MVC 4 Site Navigation

I am starting to develop a website using Spring MVC 4. I'd like my website to have a top-navigation. When a user hovers over the top-navigation I want them to see a list of the pages that they have access to.
How do I achieve this in a Spring MVC 4 best-practice way?
To illustrate my example, imagine the following:
<ul id='menu-nav'>
<li><a href='homeURL'>Home</a></li>
<li><a href='page1URL'>Page 1</a></li>
<li><a href='page2URL'>Page 2</a></li>
</ul>
Above is a list of all the pages in my very limited website. Once each user logs in, I want them to only be able to see the links to the pages that they have access to.
So, as an example, a user logging in with ADMIN rights would see ALL the links. But a user who DOES NOT have ADMIN rights would only see links to the Home Page and Page 1.
Can anyone suggest a way to implement this?
I think this should work :
<ul id='menu-nav'>
<li><a href='homeURL'>Home</a></li>
<li><a href='page1URL'>Page 1</a></li>
<!-- For many roles -->
<!-- <security:authorize access="hasAnyRole('ADMIN', 'USER')"> -->
<!-- For one role -->
<security:authorize access="hasRole('ADMIN')">
<li><a href='page2URL'>Page 2</a></li>
</security:authorize>
</ul>
Check this answer : https://stackoverflow.com/a/11469342/8800147

My Laravel 5.4 Logged in User Sessions wont persist

I'm having this weird issue with my app, it can't persist logged in user session using the Auth facade. Example, in my header view I put logged in user data as :
#if (Auth::check())
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<img src="{{route('getphoto', Auth::user()->image)}}" alt="" />
{{ Auth::user()->name }}
<span class="caret"></span>
</button>
</div>
#else
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Not Logged In
</button>
</div>
#endif
At the first time after login the logged in user data in the header like photo and name will be displayed correctly, but after I refresh or moving to another page they disappear and indicate that the user is not logged in. I don't know what I did wrong, all I did with user login related function is just changing the email to username. How do I solve this ? I'm using Laravel 5.4
You need to set correct permissions for the storage directory:
chmod -R 755 storage
After installing Laravel, you may need to configure some permissions. Directories within the storage and the bootstrap/cache directories should be writable by your web server.
https://laravel.com/docs/5.4/installation

How can i direct to another page in laravel (AdminLTE Template)?

i already installed template AdminLTE in my laravel app.
but i not totally understand how to direct my slidebar menu to direct page.
ex:
<li><i class='fa fa-calculator'></i> <span>Calculator</span></li>
Set in the href= url that you want redirect to.
<li><i class='fa fa-calculator'></i> <span>Calculator</span></li>

No way to open local htm file in php?

Need to open an htm page that is located on my local disk.
I'm running an application using php and xampp server.
I've tried in every way and not make it.
My application path is C:\xampp\htdocs\portal
My htm file path is C:\sistemas\test.htm
I can't move my test file and put it into portal folder.
when I click on the link just nothing happens.
here's my code
<li>
<a target="_new" href="file:///C:/SISTEMAS/test.htm">
<span class="ui-icon ui-icon-suitcase"> </span> Test
</a>
</li>
I've tried in Firefox 25 and IE10. Same result.
You cannot actually do like this but I guess your requirement is just including the content of test.htm. Using php your code can go something like this.
<li>
<a target="_new" href="test.php">
<span class="ui-icon ui-icon-suitcase"> </span> Test
</a>
</li>
and test.php would be including the content of test.htm
<?php require('../../sistemas/test.htm'); ?>

Resources