Skip to content

Instantly share code, notes, and snippets.

@mauriciogentile
Created August 3, 2015 11:38
Show Gist options
  • Save mauriciogentile/cf9a05d4876aa7e63ac2 to your computer and use it in GitHub Desktop.
Save mauriciogentile/cf9a05d4876aa7e63ac2 to your computer and use it in GitHub Desktop.
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