How to implement grails bean - grails

In my grails application, there are ten domain classes, in every domain class there is one comment field which is common. It compromises of current authenticated user with current time-stamp.
How can I implement the above said comments using beans

Create a comment Domain class
class comment
{
String message;
static belongsTo=[User] //add or can leave it , for all your ten domains
}
Then you need to associate it with let say to Ten of your domain class ,ex.User
class User {
String UserName
static hasMany=[comments:Comment] // if you have many commentin one pass or
Comment comment ///just one to one relationship for every login one record
}
And the Bean ,
You can create a commentService for just operating on the comment and your domain class,grails create a DI bean automatically after you create a commentService on services
and you could have some sample service method which will be injected
def registerInfo (){
//do some comment and domin related stuff
}
like in a controller login
def commentService
def signin(){
commentService.registerInfo(params)
}

Related

What is best way to update domain class object with large number of variables

Suppose I have Employee domain class, I want to create object of domain class from params map coming from UI side.
I can create object in two ways as follows
Normal way
Employee employee = new Employee(name: params.name, rollNo:
params.rollNo)
and so on. If domain class has 20 variables, then we need to write all variables in above constructor.
Following is best way to create object
Employee employee = new Employee(params)
Above constructor will populate object with matching params. Right.
Now my question comes here.
If suppose I have existing domain class object fetched from DB, Now I want to update this object from params map coming from UI.
What is best way to do this (like we do in above second option).
I think it is best to use command objects and bind it to the Employee.
here is sample pseudo code:
class EmployeeMgmtController {
def editEmp(EmployeeCmd cmd){
Employee editEmp = Employee.get(1)
editEmp.properties = cmd
editEmp.save()
}
}
class EmployeeCmd{
String id
static constraints = {
id blank:false,nullable:false
}
}
or,
you if your on controller, and still want to use params (and exclude any fields that you don't want to bind):
bindData(editEmp, params, [exclude:['firstName', 'lastName']])
If you want to achieve that in a service class, make your service implement grails.web.databinding.DataBinder then use the bindData method as demonstrated below.
import grails.web.databinding.DataBinder
class MyAwesomeService implements DataBinder {
/**
* Updates the given instance of a domain class to have attribute values specified
* in "newData" map.
*/
MyDomain updateMyDomainAttributes(MyDomain myDomianInstance, Map newData) {
bindData(myDomianInstance, newData)
myDomianInstance.save(flush: true)
}
}

grails and spring security acl: show only some instances of a domain class

I'm using Spring Security ACL in my Grails project to manage access into my application. I can create Admin and User to have different permissions into the application.
Now, I want that a particular user can see only some instances of a domain class object. That is:
following the example domain class object
class Patient
{
String name;
String surname;
...
}
Suppose that there are 3 created Patient objects.
I want that, if I login with
username = test1
password=test1
I can see only Patient that belongs to this User.
I think that is needed that, when I create a new Patient, it is stored that this Patient belongs to the User currently logged.
How can I do that?
EDIT:
Another problem is that, if I change the URL in the part of id to show, I can see all the Patient that are created. I want that, if I change URL manually, I see an access error. Is it possible?
EDIT 2:
How can I get the role of the user currently logged in? I've tried with the following code How to get current user role with spring security plugin? but I cannot perform the getAuthorities() because it tells me that it does not exists
I've solved EDIT2 in the following discussion grails exception: Tag [paginate] is missing required attribute [total]
I need to solve the EDIT1
thanks
If I understand you right you need to define belongsTo. This will create mapping in database from Patient to User.
Edit: to get current logged in user use
class SomeController {
def authenticateService
def list = {
def user = authenticateService.principal()
def username = user?.getUsername()
.....
.....
}
}
To map to user change logic in controller or use events to create mapping
Edit: edit create action:
class PatientController {
def authenticateService
...
def create() {
def patientInstance = new Patient(params)
patientInstance.user = authenticateService.principal()
...
[patientInstance: patientInstance]
}
...
}

Grails SpringSecurity User Object

I have a basic Grails 2 application which I setup along with the SpringSecurityCore plugin. This seems to be working fine. However, when I try to add additional properties to my User.groovy file by way of an extended class, I cannot seem to reference those properties in my controllers.
To better illustrate this problem, please take a look at my basic class which extends the User.groovy:
UserInfo.groovy:
class UserInfo extends User {
String firstname
String lastname
}
On my page where I want to reference the current user's firstname, I am simply writing a method as follows:
def index() {
def uname = springSecurityService.currentUser.username
def firstname = springSecurityService.currentUser.firstname
render uname
}
This works fine for rendering the username, and I believe that this is because the username is referenced in the base User.groovy file:
class User {
transient springSecurityService
String username
String password
However, the above "def index()" method fails when I try to define the firstname as part of the springSecurityService.currentUser.firstname.
If I inject extra properties into the base User.groovy class, then I can reference them by way of springSecurityService.currentUser.[property]
To illustrate this:
class User {
transient springSecurityService
String username
String password
String firstname
...
I am then able to reference the firstname property of my user in the aforementioned index method.
Is it possible to reference the extended properties of my User without injecting values in the base User class? My goal here is to try and keep the User class as clean as possible while still being able to call upon the values referenced in the UserInfo.groovy file.
Thank you in advance for your time.
If you really need to put some properties of your User information into another domain and want those properties could still be accessed from springSecurityService.currentUser, you have to implement your own UserDetailsService to package the UserInfo properties with User.
If you extend the User class, the SpringSecurity plugin does not know anything about your extended class UserInfo.
If you call springSecurityService.currentUser you get an instance of User not of UserInfo - so you don't have the property firstname.
If you don't like to add several new properties to User you could add a reference to the UserInfo class.
class UserInfo {
static belongsTo = [user: User]
String firstname
String lastname
...
}
class User {
...
UserInfo userInfo
...
}
You should remember that this reference adds an extra table to your database, which causes an additional join operation if you want to access your user instance.
When you initially install the Spring Security plugin and created your User class, it should have added some properties in Config.groovy that tells it which class to use as the default user class. When extending the default User class, you should update the property in Config.groovy to reflect this. The property you are looking for to update (or add if it's not there) is the following:
grails.plugins.springsecurity.userLookup.userDomainClassName = 'your.package.UserInfo'
There are about a handful of properties in this page that relate to User and Role/Authorities that you may want to update if you extend/update any of the defaults.

how to add users in spring security core

I just added the spring security plugin to my grails application. I have a question about adding users and their associated roles. I am able to do it correctly in the bootstrap but was wondering how to do it in the GSP page. I have a gsp page with the corresponding fields. when submited, it call the save method. my user domain controller extends SecUser. below is the example:
class User extends SecUser {
String fname
String lname
Date dateCreated
Date lastUpdated
static constraints = {
fname (blank:false)
lname (blank:false)
}
String toString(){
fname & " " & lname
}
}
When the user is saved, it saves only items in the user domain, not the SecUser. Does anyone have an example GSP and controller code to save the all the user data?
I perceive that you have two problems :
How to create a Crud (including gsp) on your User class
How to persist the data on your class and its inherited fields
For the first question:
To create GSPs and everything you need to have a CRUD on your User class, I suggest that you use scaffolding. If will take care of all of this for you.
Remove everything from your UserController or create another controller with only the following code:
class UserController {
static scaffold = User
}
then, navigate to your UserController ({your_app}/user/index) and everything should be there.
If you want to have an actual controller and gsps and modify how they work, use the grails command :grails generate-all your.package.User
For the second question :
Unless there is a problem with your SecUser class (transient fields for example), all fields inherited from SecUser should be saved through a User.save()
Let me know how it goes,
Vincent Giguère
Did you use the included script to generate your User class?
grails s2-quickstart
DOMAIN_CLASS_PACKAGE USER_CLASS_NAME
ROLE_CLASS_NAME
[REQUESTMAP_CLASS_NAME]
eg.
grails s2-quickstart com.yourapp User Authority
When I did that in my grails app, the resulting User class did not extend SecUser.

Make a Linq-to-SQL Generated User Class Inherit from MembershipUser

I am currently building a custom Membership Provider for my Asp.net MVC website. I have an existing database with a Users table and I'm using Linq-to-Sql to automatically generates this class for me.
What I would like to do is have this generated User class inherit from the MembershipUser class so I can more easily use it in my custom Membership Provider in methods such as GetUser. I already have all the necessary columns in the table.
Is there any way to do this? Or am I going about this the completely wrong way?
Thanks!
Usually code generation tools creates so called partial classes, like:
public partial class User
{
// class definition here
}
This means that you can extend definition of that class somewhere within the same namespace like that:
public partial class User: MembershipUser
{
// if MembershipUser doesn't have parameterless constructor then you need
// to add here one
}
And then you'll have User class inheriting from MembershipUser.

Resources