Created
August 3, 2015 11:38
-
-
Save mauriciogentile/cf9a05d4876aa7e63ac2 to your computer and use it in GitHub Desktop.
This file contains 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
public class SecuredActivity : CodeActivity | |
{ | |
[RequiredArgument] | |
public string Roles { get; set; } | |
protected override void Execute(CodeActivityContext context) | |
{ | |
EnforceSecurity(); | |
} | |
void EnforceSecurity() | |
{ | |
var ok = new Func<bool>(() => | |
{ | |
var roles = Roles.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); | |
var orgName = AppContext.Current.OrganisationName; | |
return roles.Any(r => | |
{ | |
var role = string.Format("{0}:{1}", orgName, r); | |
return AppContext.Current.User.IsInRole(role); | |
}); | |
}); | |
if (!AppContext.HasContext || !ok()) | |
{ | |
throw new UnauthorizedAccessException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment