sql server - How to Grant Permission to IMPERSONATE any other user? -
in order log usage of application developing need every user using application execute queries against sql server database under own credentials.
in order not storing passwords in retrievable fashion, can't creating connection on per-user basis (because entail knowing password past brief window when log-in).
the, seemingly obvious, solution problem (which may sub-optimal) run sensitive queries generic "application" user, impersonating logged in user (requiring me associate logged in user username...which not bad thing).
my problem i'm not sure how grant impersonate users of role, or users in general (not brightest idea, because don't want app impersonating sysadmin, instance).
grant impersonate on applicationlogin doesn't work, , there's no documentation can find suggests granting impersonation on members of role doable...
any ideas?
you can use dynamic sql . code below fetches users related specific role , grant permission impersonate on user. should create user on application login relate database , grant permission impersonate on members of specific role. code:
create trigger s2 on database create_user create table #t (principal_name nvarchar(100),role_name nvarchar(100)); l (select * (select p.name 'principal_name',r.role_principal_id 'gh' sys.database_principals p,sys.database_role_members r p.principal_id=r.member_principal_id or p.principal_id=r.role_principal_id , type<>'r') s inner join (select p.name 'role_name',p.principal_id 'gha' sys.database_principals p,sys.database_role_members r p.principal_id=r.member_principal_id or p.principal_id=r.role_principal_id , type='r') d on d.gha=s.gh) insert #t select distinct principal_name,role_name l ------------ enter role name here role_name '%%' ------------ declare @p nvarchar(100),@text nvarchar(max)='' ------------------------- change desired name of application user declare @appuser nvarchar(100)='application_user' ------------------------- declare c cursor select principal_name #t open c fetch next c @p while(@@fetch_status=0) begin set @text+='grant impersonate on user::['+@p+'] '+@appuser+' ' fetch next c @p end close c deallocate c drop table #t exec(@text) i hope work you.
Comments
Post a Comment