เนื่องจากไม่มีอะไรทำ ก็เลยลองนั่งทำเวปแนวๆ twitter แต่เปลี่ยน concept ใหม่ เนื่องจากผมว่าเบื่อที่บางคน twit จนกลายเป็น chatroom ไปกลายๆ ก็เลยทำให้มันเปลี่ยนจากอิง follow จาก user กลายเป็น follow เป็น group ไปแทน
ก็เลยนั่งเขียน grails แล้วในส่วนของการจัดการการ authen ก็เลยใช้ acegi plugin ของ grails เข้ามาช่วย
เริ่มแรกก็ทำการสร้าง Application
# grails create-app bookstore
# cd bookstore
จากนั้นทำการลง plugin
# grails install-plugin acegi
จากนั้นทำการสร้าง domain User, Role และ Requestmap โดยหากต้องการเปลี่ยนชื่อ ก็สามารถทำได้ แต่โดนปกติเค้ามักจะใช้ชื่อนี้กันเลย
# grails create-auth-domains User Role Requestmap
หลังจากสั่งจะได้ domain User, Role และ Requestmap ดังนี้
User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class User { static transients = ['pass'] static hasMany = [authorities: Role] static belongsTo = Role String username String userRealName String passwd boolean enabled String email boolean emailShow String description = '' String pass = '[secret]' static constraints = { username(blank: false, unique: true) userRealName(blank: false) passwd(blank: false) enabled() } } |
Role
1 2 3 4 5 6 7 8 9 10 11 | class Role { static hasMany = [people: User] String description String authority static constraints = { authority(blank: false, unique: true) description() } } |
Requestmap
1 2 3 4 5 6 7 8 9 | class Requestmap { String url String configAttribute static constraints = { url(blank: false, unique: true) configAttribute(blank: false) } } |
หลังจากนั้นหากต้องการสร้าง Controller และ View ก็สามารถสั่งได้โดย
#grails generate-all User
#grails generate-all Role
#grails generate-all Requestmap
แต่หากให้ง่ายกว่าก็สามารถทำได้โดยคำสั่งเดียวคือ
# grails generate-manager
คำั่สั่งนี้จะไปสร้างให้เองหมดเลย
หากต้องการสร้างหน้าจอ Register ก็สั่งให้มันสร้างให้เหมือนกัน แต่ว่าหน้าจอมันก็จะเป็นแบบง่ายๆ และมีการเรียกใช้ CAPTCHA ให้ด้วย
# grails generate-registration
สุดท้ายก็มาลองกัน โดยการรันโปรเจ็คโดย
#grails run-app
source: http://www.grails.org/AcegiSecurity%20Plugin%20-%20Basic%20Tutorial
