Cool thanks that helps.
I'm trying to set the permissions so that only one role of my choosing can view them. The code seems to be executing correctly, but when viewing the permissions in the backend pages it still looks like it is when having default permissions.
However the code must have done something, because when trying set the permissions in the backend to the role i specified in my code i get an error saying return code 0, but checking any other role works ok.
Do I need to deny everyone first?
var userManager = UserManager.GetManager(
"Default"
);
userManager.Provider.SuppressSecurityChecks =
true
;
// Get the current logged in user.
MembershipUser currentUser = Membership.GetUser();
string
loginName = currentUser.UserName;
var user = userManager.GetUser(loginName);
userManager.Provider.SuppressSecurityChecks =
false
;
// Get the user's role
RoleManager roleManager = RoleManager.GetManager(RoleManager.GetDefaultProviderName());
Role myRole = roleManager.GetRoles().Where(r => r.Name ==
"MyRole"
).Single();
// Document
string
albumTitle =
"MyAlbum"
;
string
documentTitle =
"DocTitle"
;
LibrariesManager manager = LibrariesManager.GetManager();
DocumentLibrary docLibToLocate = manager.GetDocumentLibraries()
.Where(dL => dL.Title == albumTitle).Single();
IQueryable<Document> docToUse = App.WorkWith().DocumentLibrary(docLibToLocate.Id)
.Documents()
.Where(d => d.Title == documentTitle).Get();
Document doc = docToUse.FirstOrDefault();
Permission privatePermission = manager.CreatePermission(
SecurityConstants.Sets.Document.SetName, doc.Id, myRole.Id);
privatePermission.GrantActions(
false
, SecurityConstants.Sets.Document.View);
doc.Permissions.Add(privatePermission);
manager.SaveChanges();