-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCrear roles RLS
92 lines (68 loc) · 3.77 KB
/
Crear roles RLS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Codigo Adaptado de https://www.elegantbi.com/post/modelautobuild
//Muchas gracias a Michael Kovalsky por sus enormes aportes
// Este script lee la información para crear roles desde una tabla auxiliar en el modelo y crea todos los roles.
// Todos estos Scripts funcionan en el modelo de AdventureWorks de Ejemplo
//RolesAuxliar debe ser una tabla con 3 columnas, nombre del rol, tabla y filtro RLS,
//debe tener una fila por cada combinación de Rol y Tabla a asegurar
//RoleName TableName FilterExpression
//Group 1 Product 'Product'[Color] in { "RED","BLUE","BLACK"}
//Group 1 Sales Territory 'Sales Territory'[Country] in { "United States","France"}
//Group 2 Product 'Product'[Color] in { "RED","YELLOW"}
string query = "EVALUATE RolesAuxiliar";
using (var reader = Model.Database.ExecuteReader(query)){
//Create a loop for every row in the resulset
while(reader.Read()){
string MyRol = reader.GetValue(0).ToString();
string rls = reader.GetValue(2).ToString();
string Table = reader.GetValue(1).ToString();
if (!Model.Roles.ToList().Any(x => x.Name == MyRol))
{
var obj = Model.AddRole(MyRol);
obj.ModelPermission = ModelPermission.Read;
}
Model.Tables[Table].RowLevelSecurity[MyRol] = rls;
}
}
// Fin Script 1
--------------------------------------------------------------------------------------------
//// Esta variacion crea los roles en función de los campos de tablas del modelo de forma dinámica
string query = "EVALUATE FILTER(CROSSJOIN(VALUES('Product'[Color]),VALUES('Sales Territory'[Country])),NOT(ISBLANK('Product'[Color]))) ORDER BY 'Product'[Color]";;
using (var reader = Model.Database.ExecuteReader(query)){
//Create a loop for every row in the resulset
while(reader.Read()){
string Color = reader.GetValue(0).ToString();
string Pais = reader.GetValue(1).ToString() ;
string MyRol = "Rol con permisos en color " + Color + " y pais " + Pais ;
string rls_color = "Product[color]=\"" + Color + "\"" ;
string rls_pais = "'Sales Territory'[country]=\"" + Pais + "\"" ;
if (!Model.Roles.ToList().Any(x => x.Name == MyRol))
{
var obj = Model.AddRole(MyRol);
obj.ModelPermission = ModelPermission.Read;
}
Model.Tables["Product"].RowLevelSecurity[MyRol] = rls_color;
Model.Tables["Sales Territory"].RowLevelSecurity[MyRol] = rls_pais;
}
}
//// Esta variacion crea los roles en función de los campos de tablas del modelo de forma dinámica,
//pero con una tabla muy grande ( todos los valores de producto, solo por diversion, a ver hasta cuantos roles se pueden crear en Power BI
// Este script genera mas de 20K roles
string query = "EVALUATE FILTER(CROSSJOIN(VALUES('Product'[Color]),VALUES('Sales Territory'[Country]), VALUES('Product'[ProductKey])),NOT(ISBLANK('Product'[Color]))) ORDER BY 'Product'[Color]";
using (var reader = Model.Database.ExecuteReader(query)){
//Create a loop for every row in the resulset
while(reader.Read()){
string Color = reader.GetValue(0).ToString();
string Pais = reader.GetValue(1).ToString() ;
string Producto = reader.GetValue(2).ToString() ;
string MyRol = "Rol con permisos en color " + Color + " y pais " + Pais + "y producto " + Producto ;
string rls_color = "Product[color]=\"" + Color + "\"" + "&& Product[product]=" + Producto;
string rls_pais = "'Sales Territory'[country]=\"" + Pais + "\"" ;
if (!Model.Roles.ToList().Any(x => x.Name == MyRol))
{
var obj = Model.AddRole(MyRol);
obj.ModelPermission = ModelPermission.Read;
}
Model.Tables["Product"].RowLevelSecurity[MyRol] = rls_color;
Model.Tables["Sales Territory"].RowLevelSecurity[MyRol] = rls_pais;
}
}