Grails:字符串(用户名)作为主键,但保存并获取id字符串(用户名)与ignorecase?(Grails: String(username) as primary key, but save a

编程入门 行业动态 更新时间:2024-10-24 00:21:21
Grails:字符串(用户名)作为主键,但保存并获取id字符串(用户名)与ignorecase?(Grails: String(username) as primary key, but save and get id string(username) with ignorecase?)

我使用字符串“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 allowed

When 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' anyway

Domain 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.

更多推荐

本文发布于:2023-08-01 02:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353732.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   用户名   主键   ignorecase   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!