数据库: Server。
我有一个场景,我需要在每周的几天内授予用户系统访问权限。
我的意思是,假设用户A可以在一周中的7天访问系统,但是用户B只能在星期一、星期三和星期五访问系统,用户C只能在星期日、星期二、星期四访问系统。
如何为此场景设计表?
我设计了一张桌子。
用户
userId int not null,
userName varchar(25),
userStatus varchar(1)现在如何实现上述场景?请建议一下。
发布于 2016-03-27 15:01:33
我将在主database.Table中使用一个表,其中包含用户名(与登录名相同),以及它们可以访问的天数。
create table usersacess
(
username varchar(30),
accessdays char(10)
)
insert into usersacess
select 'testlogin1','sunday'
union all
select 'testlogin','monday'
union all
select 'testlogin','tuesday'一旦您有了上述信息,您需要创建一个登录触发器,您也可以选择登录到DB。
alter TRIGGER connection_limit_trigger
ON ALL SERVER with execute as 'sa'
FOR LOGON
AS
BEGIN
if exists(select 1 from usersacess where username=original_login())
begin
if not exists(select 1 from usersacess where username=ORIGINAL_LOGIN() and accessdays=datename(dw,getdate()))
begin
rollback;
end
end
END;需要注意的几点:
1.使用Execute as SA很重要,因为有些登录可能无法读取usersaccess表 2.首先要确保,我们不是在检查所有用户 3. usersacess表中的登录名应与为用户创建的原始名称相匹配。
现在,如果您尝试测试,testlogin1今天只能访问,因为现在是星期天。
https://stackoverflow.com/questions/36246526
复制相似问题