User NoAccess programatically

Vote:
 

Hi,

I am trying to delete or set NoAccess on a set of users programatically. My problem is that my code works fine for 1 user but when i try the same code on multiple users only one of them are set to NoAccess. Anyone else had this problem? Using CMS 5.

 My code:

 foreach (string user in users)
                {
                    if (page.ACL.Exists(user))
                    {
                        AccessControlList writeList = page.ACL.CreateWritableClone();
                        AccessControlEntry entry = new AccessControlEntry(user, AccessLevel.NoAccess, SecurityEntityType.User);
                        writeList[user] = entry;
                        writeList.Save();
                    }
                }

#24631
Oct 02, 2008 14:30
Vote:
 

In addition:

The code is fired through a button click. If i have f.ex two users that i want to remove, the first one is removed when i click the button. but i have to click the button again to remove the last one.

Anyone got a clue?

-Kjetil Simensen

#24632
Oct 02, 2008 14:35
Vote:
 

Solved!

After a lot of try and errors I finally managed to get this thing to work! My mistake in the initial code might be obvious (maybe that's why nobody came to my rescue, hehe) but I am very pleased nevertheless. The mistake was that I created a new writableclone of the ACL list for each user and saved it, instead of only creating one writeableclone before my "foreach" loop and save it afterward. Looking back I'm a bit embarrassed for not trying this earlier...

The final code:

AccessControlList writeList = page.ACL.CreateWritableClone(); 

foreach (string user in users)
                {
                    if (writeList .Exists(user))
                    {
                        
                        AccessControlEntry entry = new AccessControlEntry(user, AccessLevel.NoAccess, SecurityEntityType.User);
                        writeList[user] = entry;
                     }
                }

writeList.Save();

-Kjetil Simensen

#24741
Oct 07, 2008 10:02