CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
AuthorizationRules.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="AuthorizationRules.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>IsInRole authorization rule.</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12
14{
19 {
20 private List<string> _roles;
21
27 public IsInRole(AuthorizationActions action, List<string> roles)
28 : base(action)
29 {
30 _roles = roles;
31 }
32
38 public IsInRole(AuthorizationActions action, params string[] roles)
39 : base(action)
40 {
41 _roles = new List<string>(roles);
42 }
43
50 public IsInRole(AuthorizationActions action, Csla.Core.IMemberInfo element, List<string> roles)
51 : base(action, element)
52 {
53 _roles = roles;
54 }
55
62 public IsInRole(AuthorizationActions action, Csla.Core.IMemberInfo element, params string[] roles)
63 : base(action, element)
64 {
65 _roles = new List<string>(roles);
66 }
67
72 protected override void Execute(IAuthorizationContext context)
73 {
74 if (context.ApplicationContext.User != null)
75 {
76 if (_roles.Count > 0)
77 {
78 foreach (var item in _roles)
79 if (context.ApplicationContext.User.IsInRole(item))
80 {
81 context.HasPermission = true;
82 break;
83 }
84 }
85 else
86 {
87 context.HasPermission = true;
88 }
89 }
90 }
91 }
92
97 {
98 private List<string> _roles;
99
105 public IsNotInRole(AuthorizationActions action, List<string> roles)
106 : base(action)
107 {
108 _roles = roles;
109 }
110
116 public IsNotInRole(AuthorizationActions action, params string[] roles)
117 : base(action)
118 {
119 _roles = new List<string>(roles);
120 }
121
128 public IsNotInRole(AuthorizationActions action, Csla.Core.IMemberInfo element, List<string> roles)
129 : base(action, element)
130 {
131 _roles = roles;
132 }
133
140 public IsNotInRole(AuthorizationActions action, Csla.Core.IMemberInfo element, params string[] roles)
141 : base(action, element)
142 {
143 _roles = new List<string>(roles);
144 }
145
150 protected override void Execute(IAuthorizationContext context)
151 {
152 context.HasPermission = true;
153 if (context.ApplicationContext.User != null)
154 {
155 foreach (var item in _roles)
156 if (context.ApplicationContext.User.IsInRole(item))
157 {
158 context.HasPermission = false;
159 break;
160 }
161 }
162 }
163 }
164}
IPrincipal User
Get or set the current IPrincipal object representing the user's identity.
Base class providing basic authorization rule implementation.
IsInRole authorization rule.
IsInRole(AuthorizationActions action, List< string > roles)
Creates an instance of the rule.
IsInRole(AuthorizationActions action, Csla.Core.IMemberInfo element, params string[] roles)
Creates an instance of the rule.
IsInRole(AuthorizationActions action, params string[] roles)
Creates an instance of the rule.
IsInRole(AuthorizationActions action, Csla.Core.IMemberInfo element, List< string > roles)
Creates an instance of the rule.
override void Execute(IAuthorizationContext context)
Rule implementation.
IsNotInRole authorization rule.
IsNotInRole(AuthorizationActions action, List< string > roles)
Creates an instance of the rule.
override void Execute(IAuthorizationContext context)
Rule implementation.
IsNotInRole(AuthorizationActions action, Csla.Core.IMemberInfo element, List< string > roles)
Creates an instance of the rule.
IsNotInRole(AuthorizationActions action, params string[] roles)
Creates an instance of the rule.
IsNotInRole(AuthorizationActions action, Csla.Core.IMemberInfo element, params string[] roles)
Creates an instance of the rule.
Maintains metadata about a method or property.
Definition: IMemberInfo.cs:19
Implemented by objects which provide context information to an authorization rule when it is invoked.
bool HasPermission
Gets or sets a value indicating whether the current user has permission to perform the requested acti...
ApplicationContext ApplicationContext
Gets a reference to the current ApplicationContext.
AuthorizationActions
Authorization actions.