public static AccessControlToken GetNewItemPermissionsToken(Core core, IPermissibleItem item, string fieldName) { AccessControlToken token = new AccessControlToken(core, item); List<PrimitivePermissionGroup> groups = PermissionGroupSelectBox.FormPermissionGroups(core, fieldName); token.AddGroups(groups); return token; }
/// <summary> /// Creates a new blog entry /// </summary> /// <param name="core">Core token</param> /// <param name="blog"></param> /// <param name="title">Title for the new blog entry</param> /// <param name="body">Body for the new blog entry</param> /// <param name="license">License ID for the new blog entry</param> /// <param name="status">Publish status for the new blog entry</param> /// <param name="category">Category ID for the new blog entry</param> /// <param name="postTime">Post time for the new blog entry</param> /// <returns>The new blog entry retrieved from the DB</returns> /// <exception cref="NullCoreException">Throws exception when core token is null</exception> /// <exception cref="InvalidBlogException">Throws exception when blog token is null</exception> /// <exception cref="UnauthorisedToCreateItemException">Throws exception when unauthorised to create a new BlogEntry</exception> public static BlogEntry Create(Core core, AccessControlToken token, Blog blog, string title, string body, byte license, PublishStatuses status, short category, long postTime) { if (core == null) { throw new NullCoreException(); } if (blog == null) { throw new InvalidBlogException(); } if (blog.UserId != core.LoggedInMemberId) { throw new UnauthorisedToCreateItemException(); } /*if (!blog.Access.Can("POST_ITEMS")) { }*/ string bodyCache = string.Empty; if (!body.Contains("[user") && !body.Contains("sid=true]")) { bodyCache = core.Bbcode.Parse(HttpUtility.HtmlEncode(body), null, blog.Owner, true, string.Empty, string.Empty); } long now = UnixTime.UnixTimeStamp(); BlogEntry blogEntry = (BlogEntry)Item.Create(core, typeof(BlogEntry), new FieldValuePair("user_id", blog.UserId), new FieldValuePair("post_time_ut", now), new FieldValuePair("post_title", title), new FieldValuePair("post_published_ut", postTime), new FieldValuePair("post_modified_ut", now), new FieldValuePair("post_ip", core.Session.IPAddress.ToString()), new FieldValuePair("post_text", body), new FieldValuePair("post_text_cache", bodyCache), new FieldValuePair("post_license", license), new FieldValuePair("post_status", (byte)status), new FieldValuePair("post_category", category), new FieldValuePair("post_simple_permissions", true)); AccessControlLists acl = new AccessControlLists(core, blogEntry); acl.SaveNewItemPermissions(token); return blogEntry; }
public void SaveNewItemPermissions(AccessControlToken token) { if (itemPermissions == null) { itemPermissions = GetPermissions(core, item); } if (itemGrants == null) { itemGrants = AccessControlGrant.GetGrants(core, item); } if (unsavedGrants == null) { unsavedGrants = new List<UnsavedAccessControlGrant>(); } if (itemPermissions != null) { List<PrimitivePermissionGroup> groups = token.Groups; if (groups.Count > 0) { foreach (AccessControlPermission itemPermission in itemPermissions) { List<ItemKey> keysGranted = new List<ItemKey>(); foreach (AccessControlGrant grant in itemGrants) { if (grant.PermissionId == itemPermission.Id) { if (grant.Allow == AccessControlGrants.Allow) { keysGranted.Add(grant.PrimitiveKey); } } } List<ItemKey> keysPosted = new List<ItemKey>(); if (itemPermission.PermissionType == PermissionTypes.View || itemPermission.PermissionType == PermissionTypes.Interact) { foreach (PrimitivePermissionGroup ppg in groups) { bool flag = true; if (ppg.ItemKey.Equals(User.GetEveryoneGroupKey(core)) && itemPermission.PermissionType == PermissionTypes.Interact) { flag = false; // Add registered users instead of everyone for interact by default if (!keysGranted.Contains(User.GetRegisteredUsersGroupKey(core))) { AccessControlGrant newACG = AccessControlGrant.Create(core, User.GetRegisteredUsersGroupKey(core), item.ItemKey, itemPermission.Id, AccessControlGrants.Allow); itemGrants.Add(newACG); } keysPosted.Add(User.GetRegisteredUsersGroupKey(core)); } if (flag) { // Only create if not exists if (!keysGranted.Contains(ppg.ItemKey)) { AccessControlGrant newACG = AccessControlGrant.Create(core, ppg.ItemKey, item.ItemKey, itemPermission.Id, AccessControlGrants.Allow); itemGrants.Add(newACG); } keysPosted.Add(ppg.ItemKey); } } } if (!keysGranted.Contains(item.Owner.ItemKey)) { AccessControlGrant newACG = AccessControlGrant.Create(core, item.Owner.ItemKey, item.ItemKey, itemPermission.Id, AccessControlGrants.Allow); itemGrants.Add(newACG); } if (!keysPosted.Contains(item.Owner.ItemKey)) { keysPosted.Add(item.Owner.ItemKey); } List<AccessControlGrant> grantsGrandfathered = new List<AccessControlGrant>(); foreach (AccessControlGrant grant in itemGrants) { if (grant.PermissionId == itemPermission.Id) { if (!keysPosted.Contains(grant.PrimitiveKey)) { grantsGrandfathered.Add(grant); } } } foreach (AccessControlGrant grant in grantsGrandfathered) { itemGrants.Remove(grant); grant.Delete(); } } item.IsSimplePermissions = true; item.Update(); } } }