Showing posts with label grails. Show all posts
Showing posts with label grails. Show all posts

Thursday, January 3, 2013

Grails: How to Read Properties Files in a Groove Class

Reading value from a *.properties file on a *.gpg page is very easy. Also, the scaffold generated CRUD pages have a lot of example what can be consulted as a start point.
Let’s suppose that we have a <my project>/i18n/parameters.properties file which has line
myparameter.something=My Value
So, if we want to get the ‘My Value’ value on a *.gpg page we just need write:
${message(code: 'myparameter.something', default: 'Some default value')}
But, what to do if it’s necessary reading a property’s value in a Groove Class? The answer is quite easy! Supposing the same parameters.properties file we supposed before, we can do:
import org.springframework.context.i18n.LocaleContextHolder as LCH
class MyClass {
 /** Dependency injection for getMessage. */
 def messageSource

 public def getMyParameter(){
  return messageSource.getMessage('myparameter.something', null, 'Some default value', LCH.getLocale())
 }
}
That's all folks!

Tuesday, November 27, 2012

Grails: How to Configure the LDAP Plugin to Work With the Active Directory

The LDAP authentication support for the Spring Security plugin provides access to directory services via LDAP. That's really easy to configure. Once you learn how to do it you'll not spent more than 10 minutes to get it working as well. In this post I'll show you how to have your Active Directory authentication service working very quickly.

To that post let's suppose that we have a Active Directory server running with the follow access data:
Address: 192.168.0.1
Port: 389
Domain: mydomain.com
Admin user: admin
Admin password: pass

So, the first thing we need to do is install the LDAP authentication plugin:
grails install-plugin spring-security-ldap

The command above will install the LDAP authentication support for the Spring Security plugin as your dependency, the Spring Security Core plugin.

After that, we can already configure our communication with the Active Directory server. On conf/config.groovy file, write the follow lines and adapt them for your access data:

grails.plugins.springsecurity.ldap.context.managerDn = 'admin' 
grails.plugins.springsecurity.ldap.context.managerPassword = 'pass' 
grails.plugins.springsecurity.ldap.context.server = 'ldap://192.168.0.1:389' 
grails.plugins.springsecurity.ldap.authorities.ignorePartialResultException = true
grails.plugins.springsecurity.ldap.search.base = 'dc=mydomain,dc=com' 
grails.plugins.springsecurity.ldap.search.filter = "sAMAccountName={0}"
grails.plugins.springsecurity.ldap.search.searchSubtree = true 
grails.plugins.springsecurity.ldap.auth.hideUserNotFoundExceptions = false 
grails.plugins.springsecurity.ldap.search.attributesToReturn = ['mail', 'displayName'] // extra attributes you want returned
grails.plugins.springsecurity.providerNames = ['ldapAuthProvider', 'anonymousAuthenticationProvider']

// role-specific LDAP config
grails.plugins.springsecurity.ldap.useRememberMe = false 
grails.plugins.springsecurity.ldap.authorities.retrieveGroupRoles = true 
grails.plugins.springsecurity.ldap.authorities.groupSearchBase = 'dc=mydomain,dc=com' 
grails.plugins.springsecurity.ldap.authorities.groupSearchFilter = 'member={0}'

If you've configured your server correctly your Grails application can already communicate with your AD server. So, now we can create our log-in page. But don't worry, Grails and its plugins do the hard work for us. For that, just run the follow command to create your login page:
grails s2-quickstart <your package, e.g. com.myproject> User Role

That command will create the User, Rule and UserRole domain classes, as well, its controllers and views. The views will be created in the views/login folder.

Now you must be able to access your login page that will authenticate your users checking them on the Active Directory. Try access something like that:
http://localhost:8080/<your-project>/login


If you want to do some customization you can check the User, Rule, UserRole domains, controllers and views for that.

Hope that help you!

Monday, November 26, 2012

How to Add DataTables Plugin in Grails

DataTables is an awesome plugin for the JQuery. It provides many features as filter and sort without you have to code them at all. In this post I'll show you the easiest way to use the DataTables plugin in Grails.

First, install the jquery-datatables plugin on your project:

grails install-plugin jquery-datatables

After, download the most reticently  DataTables package and copy the following files to your web-app project folder:

css/jquery.dataTables.css
images/*
js/jquery.dataTables.js

On the views/layouts/main.gsp file, add the following lines:

Within the head tag:

<link rel='stylesheet' href="${resource(dir: 'css', file: 'jquery.dataTables.css')}" type="text/css"/> 

<g:javascript>
   $(document).ready(function() {
      $("#dataTablesList").dataTable();
   });
</g:javascript>

Before the </body> tag:

<g:javascript library="datatables" src="jquery.dataTables.js"/>

Finally, add the dataTablesList id on the table tag of your list.gpg file:

<table id="dataTablesList">

If everything goes fine, your default list view will be able as a DataTables component and so, you will have something like that:

http://www.datatables.net/release-datatables/examples/basic_init/zero_config.html

Note that both, Grails default list and DataTables plugin have a pagination feature. So, maybe you will want to disable the Grails pagination to let the DataTables do its job by itself. For that, you can pass the counting item number of your domainClass as the max item list value in your action list (on the Controller):

params.max = Math.max(max ?: <your class>.count(), 1)

Remember that this is just a simple example of how to use the DataTables plugin in Grails by getting data directly from DOM. After that, you can do your own customization.

Maybe you want to see:

- DataTables examples page;
- Using jQuery DataTables In Grails (with JSON).

Grails: How to Solve The LDAP Plugin Error While Trying Startup Your App On Tomcat

There is a bug on the LDAP Plugin 0.8.2 that makes it incompatible with the Tomcat server. If you are getting the follow error when you try startup your WAR file on Tomcat server you can try that soluction!

The Message Error

19/11/2012 10:36:08 
org.apache.catalina.core.StandardContext listenerStartGRAVE: Exception sending 
context initialized event to listener instance of class 
org.codehaus.groovy.grails.web.context.GrailsContextLoaderListenerorg.springframework
.beans.factory.BeanCreationException: Error creating bean with name 
'ldapAuthoritiesPopulator': Cannot resolve reference to bean 'contextSource' while 
setting constructor argument; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with 
name 'contextSource': Instantiation of bean failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Could not instantiate bean 
class [org.springframework.security.ldap.DefaultSpringSecurityContextSource]: 
Constructor threw exception; nested exception is java.lang.NoSuchMethodError: 
org.springframework.security.ldap.DefaultSpringSecurityContextSource.setAuthenticationStrategy(Lorg/springframework/ldap/core/support/DirContextAuthenticationStrategy;)V 
at 
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) at 
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106) at 
org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:630) at 
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148) at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035) at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939) at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) at 
org.codehaus.groovy.grails.commons.spring.ReloadAwareAutowireCapableBeanFactory.doCreateBean(ReloadAwareAutowireCapableBeanFactory.java:126) at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at 
...

The Soluction

Just replace the gldapo-0.8.2.jar and spring-ldap-1.2.1.jar lib files for the gldapo-0.8.5.jar and spring-ldap-1.3.0.jar and rename them as gldapo-0.8.2.jar and spring-ldap-1.2.1.jar on your grails project directory (<your user directory/.grails/<your grails version>/projects/<your project name>/plugins/ldap-0.8.2/lib).

So, you will need the gldapo-0.8.5.jar and spring-ldap-1.3.0.jar files. Unfortunately I don't remember when I've got them, but you can search them on the internet.