CUstomized links in gsp - grails

I have some controllers in my grails application:-
LoginController
LogoutController
SearchableController
cirnele.SearchAllController
com.ten.cirnelle.domain.CustomerController
com.ten.cirnelle.domain.ProjectController
com.ten.cirnelle.domain.PurchaseOrderController
com.ten.cirnelle.domain.QuotationController
com.ten.cirnelle.domain.ResourceController
In Config.groovy, I have provided one of my configration:-
cirnelleControllerExclusions =['Login','Search','Searchable','Resource']
and from main.gsp, I am using:-
<g:each var="c" in="${grailsApplication.controllerClasses.sort { it.fullName } }">
<g:if test="${grailsApplication.config.cirnelleControllerExclusions.contains(c.naturalName.split()[0]) == false}">
<li class="controller"><g:link controller="${c.logicalPropertyName}">${c.naturalName.split()[0]}</g:link></li>
</g:if>
</g:each>
this code is used to provide a menu like structure at the top of every view page and it is excluding 4 cotrollers, that i specified in Config.groovy to be display as a link in view pages.
but i have many users with different roles like
ROLE_PM
ROLE_SALES/BDM
ROLE_TEAMMEMBER
ROLE_ADMIN
and my requirement is that if an user with admin role logins, then he can view all the controllers as a link(except the 4) but if a user with PM role logins, then he cannot view CustomerController link and QuotationCotroller link.
so how can i customized my main.gsp to show menu links based on the role of user.
thnks

Try to use spring security plugin. There are tags for doing exactly what you want.
<sec:ifNotLoggedIn>
<g:link controller="login" action="auth">Login</g:link>
</sec:ifNotLoggedIn>
<sec:ifAllGranted roles="ROLE_USER">
<g:link class="create" controller="post" action="timeline">My Timeline</g:link>
</sec:ifAllGranted>

Related

What to add in my controller in Grails?

I'm doing an application where after log in, User gets some settings (in check-boxes) which are Boolean in my database.
I'm trying to make that every User have his own settings checked, apropose when User logs checked are those fields where the value in the database is 1. My check-box is:
<g:checkBox name="task" value="${setting?.message}" />
<label class="text2" for="calendar">Things to done</label>
What I have to write in my controller?
in your scenario i understand there exist a one to many relationship between user and setting, as you explain after user successfully login user is take it to a view where are listed all possible setting.
you can do this in the view
<g:each in="${settings}" var="setting">
<label class="checkbox">
<g:checkBox name="settings" value="${setting}" checked="${user?.settings?.contains(setting)}"/> ${setting}
</label>
</g:each>
i took this code from a similar scenario i solve someday ago you can see it here in this github repo
after sending the form in the controller you can handle in this way
def settings = params.list("settings")
you can see the full action controler example here
You could now apply some user.addToSettings or user.removeFromSetting in order to save selected settings
i hope it help you

How to get a passed "id" param through a link from a gsp file to another gsp file in grails

Suppose I have a gsp file with a link with is working with the tag "Read more. This links opens another gsp file name "blog" and if you look at the url bar, you can see my current link is "MyWebApp/post/blog/(current i value). So how do I get the (current i value) wich is actually an integer, from the new blog.gsp file?
class PostController {
def list() {
// this action calls the page that has the "Read more" link
def posts = Post.list()
[posts: posts]
}
def blog() {
// this action is triggered by the "Read more" link and
// renders your blog post where you want the current ID
def post = Post.get(params.id)
[post: post]
}
}
blog.gsp
<html>
....
${post.id}
....
</html>
UPDATE
You can probably do the following
<g:link action="blog" controller="post" params="['id': '${i}']">
Read more
</g:link>
It depends on what i is via url mappings, but I guess it would be ${params.i}
When you create links or submit forms, your information is stored in the params Map. This map is accessible by your controller and with that you can do whatever you need like passing the data to your view, or perform query's and then pass the result to the view.
The simplest way of understand the flow controller > view > controller > another view is to use grails command generate-all and check the basic crud for your domain class. See how the show and edit works.
Gregg's answer is probably what you're looking for. If it not works, maybe you're passing an invalid id. You can check if the post exists before showing the content using <g:if>, for example:
<g:if test="${post}">
id: ${post.id}
</g:if>
<g:else>
<p>This blog post don't exists.</p>
</g:else>

Combining Spring Security Tags and Struts2 Tags

I'm currently using Spring Security to manage login and sessions on my Struts2 application.
To retrieve the logged user in a JSP page I'm using the sec tag lib by importing the following to my jsp.
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
So to print my logged user i use <sec:authentication property="principal.username" />.
I'm iterating through a java.util.List received through the action called, so a iterate it through something like this:
<s:iterator value="#request.myList" var="userEmail">
My value: <s:property value="#userEmail"/>
</s:iterator>
I'm looking for a way to test the list item to check if is the same as the logged user, but I can't manage to retrieve sec inside a <s:if> tag.
Does anyone know how to combine both? and do something like
<s:if test="%{<sec:authentication property="principal.username" /> == #userEmail}">
How about just using information from session:
<s:if test="#session.SPRING_SECURITY_CONTEXT.authentication.principal.username == #userEmail">

Display breadcrumbs of pages in a website

I am very new to struts2 I want know how can I display navigation of pages in a website like
home->login->your account->xx-> etc in the current JSP page
I made it like this
<div id="nav"> <s:a action="actionName_resultName" id="nav">Retailers </s:a> - Coupon Options</div>

How to know if the user is logged in (in the GSP)

I have index.gsp that presents a page. In that page there is a button to register, and a button to log in. Once you have logged in, if the login is ok, the app drives you again to index.gsp. I need that, if the user is logged in, these buttons disappear, and instead say "Hello, [username]". I've tried with this code, but it doesn't work (it is never logged in):
In the Controller:
def dologin(){
def user=Usuario.findByUsernameAndPassword(params.username,springSecurityService.encodePassword(params.password) )
if(user){
redirect (controller:'usuario', action:'index')
}else{
flash.message=message(code:'default.user.not.found', args:[message(code: 'params.username', default:'Usuario'), params.id])
def userlogged = springSecurityService.getCurrentUser()
render view: 'index', model: [user: user]
}
}
In index.gsp (is not full, only the piece that matters):
<sec:ifNotLoggedIn>
<div id="buttons">
<div id="login">Login
</div>
<div id="register">Registrarse
</div>
</div>
</sec:ifNotLoggedIn>
<sec:ifLoggedIn>
<div id="greet">Hello!</div>
</sec:ifLoggedIn>
Any help would be appreciated.
Thank you.
It looks like you're doing your own authentication, rather than going through the spring security authentication process. The <sec:ifLoggedIn> tag relies on spring security handling the authentication.
The usual way this is handled in a spring security app is by posting the login request to /j_spring_security_check, which, behind the scenes, gets filtered by a UsernamePasswordAuthenticationFilter.
Grails and the spring security plugin make this relatively painless by providing a LoginController and auth.gsp that you can use as a starting point. Run the s2-quickstart script (which also creates User and Role domain objects), or just copy them from the spring-security-core templates directory.
I tried it simply
<sec:ifNotLoggedIn> // DO SOMTHING</sec:ifNotLoggedIn>
<sec:ifLoggedIn> // DO SOMTHING</sec:ifNotLoggedIn>
Finally did it simply
<g:if test="${session.user==null}">
<!-- Display buttons-->
</g:if>
<g:if test=${session.user!=null">
<div id="greet"> Hello, ${session.user.username}!</div>
</g:if>
Easier, i think. But, thank you for your answer, ataylor.

Resources