Difference between revisions of "Grails Plugins"

From Blue-IT.org Wiki

(3. Create a TenantMap domain class)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
All plugins can be downloaded at: [http://grails.org/plugins http://grails.org/plugins]
 
All plugins can be downloaded at: [http://grails.org/plugins http://grails.org/plugins]
 
== Multi Tenant ==
 
An introduction on the concept of multitenant can be found at:
 
* [http://en.wikipedia.org/wiki/Multitenancy http://en.wikipedia.org/wiki/Multitenancy]
 
* [http://www.itwissen.info/definition/lexikon/Multi-Tenancy-Architektur-multitenancy-architecture.html It-Wissen Multitenancy]
 
 
Documentation
 
* [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"
 
}
 
 
Later on you can use ''<nowiki>tenant[dot]</nowiki>'' to refer to the TenantMap domain class (see later on).
 
 
=== 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 {
 
 
[...]
 
 
=== 3. Create a TenantMap domain class  ===
 
This is achieved by using the command
 
grails create-dns-map
 
 
This automatically creates a domain class with the name '''''Domain'''TenantMap.groovy''.
 
 
package tenant
 
 
/**
 
  * Maps domain name to tenantId
 
  */
 
class DomainTenantMap {
 
 
  String domainName
 
  Integer mappedTenantId
 
  String name
 
 
 
  static constraints = {}
 
}
 
 
This class maps to a database table ''domain_tenant_map''.
 
 
=== 4. Build a MultiTenat environment in BootStrap.groovy ===
 
 
'''import grails.plugin.multitenant.core.util.TenantUtils'''
 
'''import org.codehaus.groovy.grails.commons.ConfigurationHolder'''
 
'''import tenant.DomainTenantMap''' // was created with grails create-domain-map
 
 
class BootStrap {
 
 
    def init = { servletContext ->
 
 
 
'''def domainName = ConfigurationHolder.config.grails.tenantDomain'''
 
        if  {
 
          '''DomainTenantMap.findByName('myweb1')'''
 
        } else {
 
          new DomainTenantMap(  name:'myweb1',
 
                                domainName:domainName,
 
                                '''mappedTenantId:1''').save(flush:true);
 
        }
 
 
The property ''domainName'' refers to the correspondent field in the database ''domain_name''.
 
The property ''mappedtenantId '' to the field name.
 
 
mysql> select * from domain_tenant_map;
 
+----+---------+-----------------+------------------+----------------+
 
| id | version | domain_name    | mapped_tenant_id | name          |
 
+----+---------+-----------------+------------------+----------------+
 
|  1 |      0 | sub.domain.tld  |                1 | myweb1        |
 
[...]
 
  
 
== Spring Security ==
 
== Spring Security ==
Line 152: Line 71:
 
     '''private currentUser()''' {
 
     '''private currentUser()''' {
 
         '''User.get(springSecurityService.principal.id)'''
 
         '''User.get(springSecurityService.principal.id)'''
 +
 +
[[Category:Java, Groovy and Grails]]

Latest revision as of 19:50, 8 January 2012

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

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)