4

In a default install of rails when you save the first domain object it gives a fancy randomized id like 785787634 or something. Grails gives 1.

What is the preferred method of making difficult-to-guess and unlikely-to-intersect auto generated ids in grails?

Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
Mikey
  • 4,692
  • 10
  • 45
  • 73

2 Answers2

5

Grails allows you to customize the id generator. see http://grails.org/doc/latest/guide/GORM.html#identity

In your case, you could consider 'uuid' or 'guid'

aldrin
  • 4,482
  • 1
  • 33
  • 50
4

A different way to do this would be to use the default IDs as provided, but then add a dedicated column using a UUID when you need public (non-secured) access to the item.

I think this would work pretty well:

class Widget {
    String uuid

    static constraints = {
        uuid unique: true
    }

    def beforeInsert() {
        // optionally, replace the dashes by adding .replaceAll('-','')
        uuid = UUID.randomUUID().toString()
    }
}

Then you could use a controller like so:

// url: app/public/widget/48b5451a-0d21-4a36-bcc0-88b129852f1b

PublicController {
    def widget() {
        Widget w = Widget.findByUuid(params.id)
        ...
    }
}

This is indexed automatically, so it's not too slow, and the UUID is only used when looking up the widget publicly. If you have a person logged in, then you can perform security checks, and just use app/widget/edit/1 or something similar.

I wouldn't rely on a "random number" as being secure by any means. Guessing numbers works even if the numbers are not sequential. Guessing a UUID is next to impossible, comparatively. However, if you have accounts with a login, authorization checks are best by far.

OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • I'm actually less concerned about security as I am intersection. Perhaps I am going about this the wrong way.. I will try to isolate the problems I am having further and post a new question. – Mikey Feb 16 '12 at 00:31