我使用字符串“username”作为表的主键,
但是,当保存并获取用户名id列时,我希望案例被忽略,以便新用户不能尝试冒充另一个用户。
例如注册新用户时
用户名= Daxon 用户名= DaXoN //这不应该被允许获取唯一的用户名时,可以在任何情况下输入并仍然可以获得。 Youtube用他们的用户名执行此操作。
例如
youtube.com/user/Daxon youtube.com/user/DaXoN //无论如何,都应该使用相同的'Daxon'配置文件域类这使用用户名作为主键
class User { String username String password static constraints = { } static mapping = { id generator: 'assigned', name: "username", type: 'string' } }然后我脚手架的控制器和意见,所以任何人都可以帮助我保存和获得唯一的用户名忽略大小写?
I am using a string "username" as the primary-key of a table,
But when saving and getting the column with the username id I want the case to be ignored so that new users can't try to impersonate another user.
e.g. When registering a new user
username = Daxon username = DaXoN //this should not be allowedWhen getting the unique username it can be typed in any case and still be obtained. Youtube do this with their usernames.
e.g.
youtube.com/user/Daxon youtube.com/user/DaXoN //Should go to the same profile of 'Daxon' anywayDomain Class This uses username as the primary key
class User { String username String password static constraints = { } static mapping = { id generator: 'assigned', name: "username", type: 'string' } }I then scaffold the controllers and views, so can anyone help me on saving and getting unique usernames with case ignored?
最满意答案
阻止第二个用户注册仅在大小写不同的名称的一种方法是在数据库层的用户名上创建不区分大小写的唯一索引。 如果您尝试保存与大小写不匹配的名称,则会得到数据库异常。 这是mysql的默认值,但对于其他数据库,类似下面的内容应该这样做:
create unique index username_csunique_idx on user(lower(username));我不知道有什么方法可以在域类DSL中指定这种类型的索引。
要查找对象,请按用户名不区分大小写查询。 例如, User.findByUsernameIlike(userName)或User.find("from User as u where lower(u.username) = ?", [userName.toLowerCase()])如果您更喜欢使用HQL,则User.find("from User as u where lower(u.username) = ?", [userName.toLowerCase()]) 。
One way you can prevent the second user from registering a name that differs only in case is to create a case insensitive unique index on username at the database layer. If you try to save a name that case-insensitively matches an existing one, you'll get a database exception. This is the default with mysql, but for other databases, something like the following should do it:
create unique index username_csunique_idx on user(lower(username));I'm not aware of any way to specify that kind of index in the domain class DSL.
To find the objects, query by username case insensitively. For example, User.findByUsernameIlike(userName), or User.find("from User as u where lower(u.username) = ?", [userName.toLowerCase()]) if you prefer HQL.
更多推荐
发布评论