Created
September 17, 2018 22:14
-
-
Save diegodfsd/234318391beca9804d59ef7dd6613b94 to your computer and use it in GitHub Desktop.
Exemplo extensão AuthorizerQueryBuilder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.RegularExpressions; | |
using Xgen.Common.Extensions; | |
namespace Xgen.QueryProcessor.Query | |
{ | |
public class AuthorizerQueryBuilder | |
{ | |
#region Private Members | |
private static readonly Regex WhereRegex = new Regex(@"where\s+(?=\()(?=((?:(?=.*?\((?!.*?\2)(.*\)(?!.*\3).*))(?=.*?\)(?!.*?\3)(.*)).)+?.*?(?=\2)[^(]*(?=\3$)))", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase); | |
#endregion | |
#region Public Methods | |
public static string Create(string query, int userId, int originType, string columnName) | |
{ | |
var singleLineQuery = query.ToSingleLine(); | |
var matches = WhereRegex.Matches(singleLineQuery); | |
var originalCriteria = string.Empty; | |
if (matches.Count > 0) | |
originalCriteria = matches[matches.Count - 1].Groups[1].Value; | |
if (originalCriteria.IsNullOrEmpty()) | |
return $"{singleLineQuery} where ({Where(userId, originType, columnName)})"; | |
return singleLineQuery.Replace(originalCriteria, $"({ExtractInnerExpression(originalCriteria)} and {Where(userId, originType, columnName)})"); | |
} | |
#endregion | |
#region Private Methods | |
private static string ExtractInnerExpression(string originalCriteria) | |
{ | |
return originalCriteria.Substring(1, originalCriteria.LastIndexOf(')')-1); | |
} | |
protected virtual string Where(int userId, int originType, string columnName) | |
{ | |
return $@"exists (select 1 | |
from [user].users __u | |
where (__u.id = {userId} | |
and (__u.type & 0x02 > 0 or __u.type & 0x04 > 0)) | |
union | |
select 1 | |
from [user].things __t | |
join [user].users __u | |
on __t.createdby = __u.id | |
where (__u.type & 0x02 = 0 | |
and __u.type & 0x04 = 0 | |
and __t.createdby = {userId} | |
and __t.origintype = {originType} | |
and __t.originId = {columnName}) | |
union | |
select 1 | |
from [user].users __u | |
join [user].userrole __ur | |
on __u.id = __ur.userid | |
join [user].roles __r | |
on __ur.roleid = __r.id | |
join [user].permissions __p | |
on __r.id = __p.roleid | |
join [user].things __t | |
on __p.thingid = __t.id | |
where (__u.type & 0x02 = 0 | |
and __u.type & 0x04 = 0 | |
and __u.id = {userId} | |
and __t.origintype = {originType} | |
and __t.originId = {columnName}))"; | |
} | |
#endregion | |
} | |
public class Authorizerx : AuthorizerQueryBuilder | |
{ | |
private readonly int _type; | |
public Authorizerx(int type) | |
{ | |
_type = type; | |
} | |
protected override string Where(int userId, int originType, string columnName) | |
{ | |
var query = base.Where(userId, originType, columnName); | |
return $"((select case when {_type} != 1 then 1 else 0 end = 1) OR {query})"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment