aspose file tools*
The moose likes Groovy and the fly likes [Grails] Passing user object into hiddenField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "[Grails] Passing user object into hiddenField" Watch "[Grails] Passing user object into hiddenField" New topic
Author

[Grails] Passing user object into hiddenField

Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
Hello!

I wirte my first, simple app emulating the gaming review portal. What I have problem with is that I have Reviews domain class that belongs to Person class ( as author of review ) genrated from Acegi Plugin ( both clases listed below ). When I want to create new Review, the app says many things, bnut mainly this error massage:


I want the app to take and use Acegi loggedInUserInfo(field:'username') variable, to give reviews proper author users, of course, as hiddenField. Could someone help me?



And here is the part of Review/Create view tha is resposible for taking the Author but is not working:

Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
Hi Karol,

What about using hidden field for person id and then in submit action load the proper user object which you can use later in your review.

If you want to send the object through the form you will have to serialize and deserialize after.However this approach is not so secure, because user can modify the object before submission.


Jakub
Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
Kuba Zygmunt wrote:
What about using hidden field for person id and then in submit action load the proper user object which you can use later in your review.


I never mixed something like that in Grails - could you propose how this code should look like or at least give some function names to read more in manual?

Kuba Zygmunt wrote:
If you want to send the object through the form you will have to serialize and deserialize after.However this approach is not so secure, because user can modify the object before submission.


Well, it's only the learning app, so can be much more easier but can't you serialize data and then secure them by, I don't know, md5 for example?
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
I will try to make an example:


and in view



in this case you save the bandwidth, you send only small int instead of the serialized object.
Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
Kuba Zygmunt wrote:


How could this be? WIth usage of loggedInUserInfo(field:'username')? Or maybe some SQL query? I never get taking session params...
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
If you match person witch acegi user by username, then you can find person using GORM


BTW you can get information from acegi anytime so there is no need to pass user information, just find a proper person before you save a review
Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
I got idea just to remade Save function in Review ( Recenzja in polish ), but it doesn't work. Here is my code:


And here is error:

Error 500: Executing action [save] of controller [gameaddict.RecenzjaController] caused exception: No such property: Person for class: gameaddict.RecenzjaController Possible solutions: session
Servlet: grails
URI: /GameAddict/grails/recenzja/save.dispatch
Exception Message: No such property: Person for class: gameaddict.RecenzjaController Possible solutions: session
Caused by: No such property: Person for class: gameaddict.RecenzjaController Possible solutions: session
Class: RecenzjaController
At Line: [24]


Any ideas on that?
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
check packages and if controller and domain class are in different packages use import.
Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
Kuba Zygmunt wrote:check packages and if controller and domain class are in different packages use import.


The tree is like this:

/main-app-folder/...
/gameaddic/ Reviews
Person
...


That means that Controller of Review IS in package ( gameaddict ), but Person is in main folder of app ( in this case, in Controllers ) - how to make import to controller in package from main folders of app. Shouldn't they be ready to use in whole app, as they are in main controllers?
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
Hi

if you use grails version 1.3.1 or newer then your domain classes will be in package named by your application. It is always better to use packages
Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
Kuba Zygmunt wrote:Hi

if you use grails version 1.3.1 or newer then your domain classes will be in package named by your application. It is always better to use packages


I use the packages. The problem is that I don't kno how to import class of Person that is otuside of app package into Review that is inside the package.
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
hi it looks like your structure is a bit messed up.

try to sort out your folders like :


if you have a domain class put it in grails-app/domain/<package> like Review and Person .
If you want to have some classes which are not domain classes put them in src/groovy/<package>/

then you can import them in grails using import <package-name>.<className>

Regards
Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
Unfortunetly, this doesn't work. Any other ideas? Th a main porblme is that Revies controller can't find the Perrsons obcjet, even where Persons Class are in the same packages.
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
It's hard to tell, so if you could pack your grails-app, put somewhere, and provide a link ( through forum or PM ), then I could check it for you.

Jakub

--edit
I reviewed your files and couple of things you could improve.

1. Put 3 classes created by Acegi plugin into package 'gameaddict' or 'security' or even 'gameaddict.security' ( yours grail app ) so you wont have problems with finding them later. You can use a command like


2. to get info in controller about current user, add authenticateSerive and then use userDomain() method. eg.


I hope it helps

Jakub
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Grails] Passing user object into hiddenField