Difference between revisions of "Grails Plugins"

From Blue-IT.org Wiki

(Create a currentUser() method in the controller)
(Multi Tenant)
Line 9: Line 9:
 
* [http://multi-tenant.github.com/grails-multi-tenant-core/guide/ http://multi-tenant.github.com/grails-multi-tenant-core/guide/]
 
* [http://multi-tenant.github.com/grails-multi-tenant-core/guide/ http://multi-tenant.github.com/grails-multi-tenant-core/guide/]
  
 +
=== 1. Declare mode in Config.groovy ===
 +
 +
// Multi Tenant
 +
tenant {
 +
// multiTenant is the default
 +
// do "grails create-dns-map" when changing tenantDomains
 +
// this stroes the dns/tenant mappings into the database
 +
resolver.request.dns.type = "db"
 +
}
 +
 +
=== 2. Declare Tenant Class ===
 +
 +
This class should be a top level class. E.g. "Organisation" hasMany "Users".
 +
 +
import grails.plugin.multitenant.core.groovy.compiler.MultiTenant;
 +
@MultiTenant
 +
class Organisation {
  
 
== Spring Security ==
 
== Spring Security ==

Revision as of 08:36, 5 September 2011

All plugins can be downloaded at: http://grails.org/plugins

Multi Tenant

An introduction on the concept of multitenant can be found at:

Documentation

1. Declare mode in Config.groovy

// Multi Tenant
tenant {

// multiTenant is the default // do "grails create-dns-map" when changing tenantDomains // this stroes the dns/tenant mappings into the database resolver.request.dns.type = "db"

}

2. Declare Tenant Class

This class should be a top level class. E.g. "Organisation" hasMany "Users".

import grails.plugin.multitenant.core.groovy.compiler.MultiTenant;
@MultiTenant
class Organisation {

Spring Security

s2-quickstart

grails s2quickstart  your.package SecUser SecRole

creates two domain classes and corresponding controllers.

Integrade domain classes

class MyDomainClass extends SecUser {

    [...]
}

BootStrap.groovy

class BootStrap {

  def dpringSecurityService

  def init = {
    
      def userRole = SecRole.findByAuthority("ROLE_USER") ?: new SecRole(authority: "ROLE_USER").save()
      def adminRole = SecRole.findByAuthority("ROLE_ADMIN") ?: new SecRole(authority: "ROLE_ADMIN").save()
      [...]
  }
  
  def users = User.list() ?: []
  if (!users) {
     def user = new User(
         [...]
         password: springSecurityService.enodePassord("myPass")
         enabled = true
     )
  }

  SecUserSecRole.create user, userRole
 
}

Annotate the controller actions

import grails.plugins.springsecurity.Secured

class DomainClassController {

   @Secured(['ROLE_USER'])
   def myAction = {
       [...]
   }

}

Create a currentUser() method in the controller

class DomainClassController {

   @Secured(['ROLE_USER'])
   def myAction = {
       def currentUser = currentuser();
       [...]

   } 
  
       [...]
   private currentUser() {
        User.get(springSecurityService.principal.id)