Showing posts with label Sample Applications. Show all posts
Showing posts with label Sample Applications. Show all posts

Login application - Validate from Database

Posted by Anantha Narayanan On 5:17 PM


Click here to see a very basic login application with hard-coded user credentials. In current article I am discussing as on how to validate from a table where login id and password is stored. The article above points to the usage of flash and session's uses. I will not discuss more about flash and session in this article.

My assumption is that a person is assigned for a login with his email id. Based on that I planned for a domain called "Person" as below (I named the application happy):

package happy
class Person {
  String firstName
  String lastName
  String emailID
  String loginPassword
  Boolean superUser

  static constraints = { 
    firstName(size:1..120,nullable:false,blank:false)
    lastName(size:1..120,nullable:false,blank:false)
    emailID(email:true,size:1..120,nullable:false,blank:false)
    loginPassword(password:true,size:6..20,nullable:false,blank:false)
  }
}


In my PersonController.groovy, I added an interceptor which should validate a session. If no session is available it should redirect me to login page.


package happy
class PersonController {
  def beforeInterceptor = [action.this.&checkUser]
  static scaffold = true
  def checkUser() {
    if(!session.user) {
      redirect(controller:'person',action:'login')
      flash.message = "Invalid Username/Password, please try again."
    }
  }
}


I have successfully added a validation for all controllers in Person. If there is no login detected the user should be redirected to a login page. Next part is to design that page.


  def login = {
  }
 
   

I have put a blank controller inside the Person controller for login. Now I have to create a gsp file for this controller to successfully work. For this, I copied the index.gsp from views folder and copied it as login.gsp inside person foler within views. I deleted the entire contents within body tag and replaced with the following code:


<body>
  <a href="#page-body" class="skip">
  <g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
  <g:render template="loginHeader"/>
  <div class="bodyContent" id="bodyContent">
    <g:if test="${flash.message}">
      <div class="message">${flash.message}
</div>
    </g:if>
  </div>
</body>


It is a simple code, please read through all lines. From <g:render> tag, I will explain. It is looking for a _loginHeader.gsp as its a template. It then displays the body content and if any messages.

The following are the contents of _loginHeader.gsp located in views/person folder.


<g:if test="${session.user}">
<div class="login">
<g:remoteLink controller="person" action="list" update="bodyContent">${session.user.emailID}</g:remoteLink> | <g:link action="logout">Logout</g:link>
</div>
</g:if>
<g:else>
<g:form action="doLogin" method="post">
<div class="login">
 <label for='emailID'>Email ID</label>
 <input id="emailID" required type='text' name='emailID' value='${person?.emailID}' />
 <label for='loginPassword'>Password</label>
 <input id="loginPassword" required type='password' name='loginPassword' value='${person?.loginPassword}' />
 <input type="submit" value="Login"></input>
 </div>
</g:form>
</g:else>


This page will be used across all pages, that is why I decided to create it as a template. It checks for a session, and if found it displays the person logged in or else displays the login input boxes.

The form which has the input boxes for email ID and password are submitting to a doLogin. This method must be defined within the PersonController.groovy as well.

def doLogin = {
  def user = Person.findWhere(emailID:params['emailID'],loginPassword:params['loginPassword'])
  session.user = user
  if (user){
    redirect(controller:'person',action:'list')
    flash.message = "Logged in as <b>$user.firstName $user.lastName ($user.emailID)</b>"
  }else{
    redirect(controller:'person',action:'login')
    flash.message = "Invalid Username/Password, please try again."
  }
}

This method extracts values from params variable which will have all input boxes value. By checking using Person.findWhere by passing these parameters (Email ID &amp; Login Password) we will get some value to user object. If user variable was null then we assume that it is an invalid login and once more redirect to login page. If the validation was successfull, this will redirect to Person/List page.

Download Application Note: The application uses Grails 2

Bug Tracker Application - biTracker

Posted by Anantha Narayanan On 3:52 PM

This application can be used to track issues/bugs which might be arising in development.

The features include:
  • User defined resource names 
  • User defined browser names (in case of browser based application for checking compatibility) 
  • User defined pages/program units 
  • User defined environments (like uat/development etc) 
  • User defined severity status 
  • Issues/bugs are searchable using searchable plugin 

This application is intented to be a starting place wherein MS-Excel sheets can be replaced and the bugs/issues could be saved to Oracle database.

The configuration for connecting to oracle can be done by modifying the file DataSource.groovy inside grails-app/conf folder. 

Download the source and execute this application and serve your clients much better.

Request your help in improving this and making it a full-fledged application. Please share your valuable comments below.

Login Application

Posted by Anantha Narayanan On 7:40 PM

Create an application

C:\GRAILS_APPLICATONS>grails create-app login-test

Now create a controller for the user to show login buttons, or once logged in we need to show some message.

C:\GRAILS_APPLICATONS\LOGIN-TEST>grails create-controller useraccount

Once the controller is created, create a gsp file index.gsp within C:GRAILS_APPLICATION\login-test\grails-app\views\useraccount. Call it index.gsp. We want this controller because by default the controller will have a def index = { } in its body. To use this we are creating the gsp with same name. You are always free to modify the def index to some name you choose for your login, say home. Below is the content of the gsp file

<html>
<head>
<title>Welcome to Grails</title>
<meta name="layout" content="main" />
<style type="text/css" media="screen">
#login{
background-color:darkred;
color:white;
text-align:right;
padding:5px 15px;
height:25px;
}
#login a{
color:white;
}
</style>
</head>
<body>
<div id="login">
<g:if test="${session.user}">
<g:if test="${flash.message}">Welcome back ${flash.message}</g:if>
<g:else>Logged in as <B>${session.user}</B></g:else>
 | <g:link action="logout">Logout</g:link>
</g:if>
<g:else>
<g:form controller="useraccount" action="login">
<label for="name">Username</label>
<input type="text" name="username"/>
<label for="password">Password</label>
<input type="password" name="password"/>
<input type="submit" value="Login"/>
</g:form>
</g:else>
</div>
</body>
</html>

Now modify your controller file (UseraccountController.groovy) as below:

package login.test
class UseraccountController {
def index = { }
def login ={
if (params.username == "grailslover" && params.password == "hi"){
flash.message = "Welcome back, <B>${params.username}</B>."
session.user = "grailslover"
}else{
flash.message = "Login failed"
}
redirect(action: 'index')
}
def logout ={
session.user = null
redirect(action: 'index')
}
}

Now run your application using grails run-app and point your browser to this URL:
http://localhost:8080/login-test/useraccount/index

Login with username grailslover and password as hi. Initially when you login you will see a message Welcome back grailslover along with a link for Logout. Refresh the page and the message changes to Logged in as grailslover.

Now click on Logout the Login page with placeholders for Username and password appears.

To Download the application, click here. If you like the tutorial, please leave your valuable comments below.

Hello World Application

Posted by Anantha Narayanan On 10:03 PM

Now heading straight to create a fresh application we will create a “Hello World” printing application. Grails creates a main folder and numerous sub-folders for each application. It is easy to create a parent folder which holds all my applications (we will be creating more than one applications in our tutorials). Create a folder c:\grails_applications and use command prompt from now on. Navigate to c:\grails_applications folder in command prompt.

Now we are ready to create our first application. Type grails create-app helloworld in command prompt.

C:\GRAILS_APPLICATONS>grails create-app helloworld

You will get a whole lot of lines of messages about creation of folders. Just ignore them for the time-being. You must have got the final line of message as follows:

Created Grails Application at C:\grails_applications\helloworld

That’s all we want now. Navigate inside helloworld folder. Now execute the following command:

C:\GRAILS_APPLICATONS\HELLOWORLD>grails create-controller hello

Once the controller is created, we are ready to modify the class to display the Hello World text. Use a text-editor and edit the following file:

C:\grails_applications\helloworld\grails-app\controllers\helloworld\HelloController.groovy

The default contents of the file will be like this:

package helloworld

class HelloController {
def index = { }
}

Modify it to look like this:

package helloworld

class HelloController {
def world = {
render "Hello World!"
}
}

Thats all required. Before we can view the results in a browser, we have to start the application. Execute the following command for this:

C:\GRAILS_APPLICATIONS\HELLOWORLD>grails run-app

Once you get the following message, its time to open the web browser.

Server running. Browse to http://localhost:8080/helloworld

Copy/Paste the URL into web browser and you will now be able to access your application. Now click on the link below Available Controllers and you will be able to see the Hello World Message.

Note: In case you are getting error saying that port 8080 is already in use, try the following command and use a different port which is not in use.

C:\GRAILS_APPLICATIONS\HELLOWORLD>grails -Dserver.port=8090 run-app
If you like the tutorial, please leave your valuable comments below.

Recommended Post Slide Out For Blogger