Thursday, November 29, 2012

LESS: The Extended CSS

Basically, LESS is an extension for CSS with dynamic behavior such variables, mixins, operations and functions. With LESS we can better organize our stylesheets and spent less time writing them, this is really helpful. Even on LESS if you don't know how to do something specifically you can just write the original CSS code for that. As a developer I can say that LESS is what lacked on CSS.

What does it do?

Let's see just a few of what we can do with LESS:
@nice-blue: #5B83AD; 
@light-blue: (@nice-blue + #111); 
#header { color: @light-blue; }

// LESS

.rounded-corners (@radius: 5px) {
  -webkit-border-radius: @radius;
  -moz-border-radius: @radius;
  -ms-border-radius: @radius;
  -o-border-radius: @radius;
  border-radius: @radius;
}

#header {
  .rounded-corners;
}
#footer {
  .rounded-corners(10px);
}
/* Compiled CSS */

#header {
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  -ms-border-radius: 5px;
  -o-border-radius: 5px;
  border-radius: 5px;
}
#footer {
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  -ms-border-radius: 10px;
  -o-border-radius: 10px;
  border-radius: 10px;
}

How to include LESS in a project?

That's easy! Just write your stylesheets as a .less file and link it to your page with the rel set to “stylesheet/less”:
<link href="styles.less" rel="stylesheet/less" type="text/css"></link>

Then download less.js from lesscss.org, and include it in the <head> element of your page:
<script src="less.js" type="text/javascript"></script>

Make sure you include your stylesheets before the script.

That way, the less stylesheet will be compiled into css stylesheet at runtime, but, if you prefer (and that is recommended), you can pre-compile your less file as well. For that install the LESS Command-line on your machine and call:
$ lessc styles.less > styles.css

Even, there are some tools where you can integrate the command-line on your IDE to automatize the process. Per example, on Grails, we have the Less CSS Resources plugin what compiles specified .less files into their .css.

So...

So that was just a view on LESS, if you want to really know about it visit its official site on lesscss.org.

*those examples were got from lesscss.org.

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.