Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Saving albums to a role in another class
RyanONeill
#1 Posted : Monday, 15 March 2010 11:20:39 PM(UTC)
Rank: Member

Joined: 9/03/2010(UTC)
Posts: 4
Location: UK

We've got our own user admin section and have put some code in to manage the list of albums per role where the user can tick/untick the albums required for that role.

I'm trying to persist the selections to the database but not having any luck. Can you point out where the code below might be going wrong? I think I have the wrong end of the stick somewhere.

Quote:
public void Save()
{
IGalleryServerRole gsr = GetRole(RoleName);

gsr.ClearAllAlbumIds();
foreach (TreeViewItemModel Node in checkedNodes)
{
gsr.AddToAllAlbumIds(Convert.ToInt32(Node.Value));
}

gsr.Save();
}


Thanks in advance

Ryan
Roger Martin
#2 Posted : Tuesday, 16 March 2010 12:20:01 AM(UTC)
Roger Martin

Rank: Administration

Joined: 3/08/2007(UTC)
Posts: 3,300
Location: Fort Atkinson, WI

You need to populate the RootAlbumIds property, not the AllAlbumIds property. Refer to the documentation (i.e. the XML comments in the source code) for the difference between the two.

Your code will look similar to the RoleController.UpdateRoleAlbumRelationships function:

Code:
/// <summary>
/// Replace the list of root album IDs for the <paramref name="role"/> with the album ID's specified in
/// <paramref name="topLevelCheckedAlbumIds"/>. Note that this function will cause the AllAlbumIds property
/// to be cleared out (Count = 0). The property can be repopulated by calling <see cref="IGalleryServerRole.Save"/>.
/// </summary>
/// <param name="role">The role whose root album/role relationships should be updated. When editing
/// an existing role, specify this.GalleryRole. For new roles, pass the newly created role before
/// saving it.</param>
/// <param name="topLevelCheckedAlbumIds">The top level checked album ids. May be null.</param>
public static void UpdateRoleAlbumRelationships(IGalleryServerRole role, IIntegerCollection topLevelCheckedAlbumIds)
{
    if (role == null)
        throw new ArgumentNullException("role");

    if (topLevelCheckedAlbumIds == null)
        topLevelCheckedAlbumIds = new IntegerCollection();

    int[] rootAlbumIdsOld = new int[role.RootAlbumIds.Count];
    role.RootAlbumIds.CopyTo(rootAlbumIdsOld, 0);

    role.RootAlbumIds.Clear();

    if (role.AllowAdministerSite)
    {
        // Administer site permission automatically applies to all albums, so all we need to do is get
        // a reference to the root album ID.
        role.RootAlbumIds.Add(Factory.LoadRootAlbumInstance().Id);
    }
    else
    {
        role.RootAlbumIds.AddRange(topLevelCheckedAlbumIds);
    }

    if (IsRoleAnAlbumOwnerRole(role.RoleName))
        ValidateAlbumOwnerRoles(role.RoleName, rootAlbumIdsOld, role.RootAlbumIds);
}


Roger Martin
Creator and Lead Developer of Gallery Server Pro
Rss Feed  Atom Feed
Users browsing this topic
Guest
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.