public void AddPermission(string logonName, string roleDefinition, bool forceBreak, bool permissionsApplyRecursively)
        {
            var user = CTX.SP1.Web.EnsureUser(logonName);

            CTX.Lae(user, true);
            if (CTX.AllRoles == null)
            {
                CTX.AllRoles = CTX.SP1.Web.RoleDefinitions;
                CTX.Lae(CTX.AllRoles, true,
                        ar => ar.Include(
                            r => r.Name
                            )
                        );
            }
            RoleDefinition roleDef;

            try
            {
                roleDef = CTX.AllRoles.Single(x => x.Name.Equals(roleDefinition, StringComparison.InvariantCultureIgnoreCase));
            }
            catch (InvalidOperationException)
            {
                throw new ArgumentException(roleDefinition + " is not the name of a valid Role Definition in this site collection.");
            }
            this.AddPermission(new SPBindingCollection(user, roleDef), forceBreak, permissionsApplyRecursively);
        }
Esempio n. 2
0
        public void GetItems(CamlQuery query, params string[] listItemProperties)
        {
            var expressions        = GetPropertyExpressionsNoType <ListItem>(listItemProperties);
            ListItemCollection col = _list.GetItems(query);

            CTX.Lae(col, true, c => c.Include(expressions));
            CTX.Lae(_list, true, l => l.ItemCount);
            this.Items     = (SPListItemCollection)col;
            this.ItemCount = _list.ItemCount;
        }
 public SPFolderCollection GetFolders()
 {
     if (!_fol.Folders.AreItemsAvailable)
     {
         CTX.Lae(_fol.Folders, true, fols => fols.Include(
                     f => f.Name, f => f.UniqueId, f => f.ServerRelativeUrl));
     }
     this.Folders = (SPFolderCollection)_fol.Folders;
     return(this.Folders);
 }
 public SPFileCollection GetFiles()
 {
     if (!_fol.Files.AreItemsAvailable)
     {
         CTX.Lae(_fol.Files, true, fis => fis.Include(
                     f => f.Name, f => f.ServerRelativeUrl, f => f.UniqueId));
     }
     this.Files = (SPFileCollection)_fol.Files;
     return(this.Files);
 }
        public void AddPermission(SPBindingCollection bindingCol, bool forceBreak, bool permissionsApplyRecursively)
        {
            if (HasUniquePermissions.HasValue && !HasUniquePermissions.Value)
            {
                if (!forceBreak)
                {
                    throw new NoForceBreakException(this.Id);
                }
                else
                {
                    SecObj.BreakRoleInheritance(true, true);
                }
            }
            else if (!HasUniquePermissions.HasValue)
            {
                throw new InvalidOperationException("The permissions for object id \"" + Convert.ToString(this.Id) + "\" cannot be modified!");
            }

            var list = new List <RoleAssignment>(bindingCol.Count);

            for (int i = 0; i < bindingCol.Count; i++)
            {
                var binding = bindingCol[i];
                var bCol    = new RoleDefinitionBindingCollection(CTX.SP1)
                {
                    binding.Definition
                };
                list.Add(SecObj.RoleAssignments.Add(
                             binding.Principal, bCol));

                foreach (var ass in list)
                {
                    CTX.Lae(ass, false);
                }
                Update();
                CTX.Lae();
            }
            if (Permissions != null)
            {
                for (int ra = 0; ra < list.Count; ra++)
                {
                    var r = list[ra];
                    CTX.Lae(r, true, ras => ras.Member.Id, ras => ras.Member.Title);
                }
                Permissions.AddRange(this, list);
            }
            else
            {
                this.GetPermissions();
            }

            //if ()
        }
        public void RemovePermission(Principal principal)
        {
            if (!HasUniquePermissions.HasValue || (HasUniquePermissions.HasValue && !HasUniquePermissions.Value))
            {
                throw new InvalidOperationException("This item does not contain unique permissions.  No permissions were removed.");
            }

            var roleAss = SecObj.RoleAssignments.GetByPrincipal(principal);

            roleAss.DeleteObject();
            CTX.Lae();
        }
Esempio n. 7
0
        public SPBinding(Principal prin, RoleDefinition def)
        {
            //bool test = prin.IsPropertyReady(p => p.Id);
            Principal res = !prin.IsPropertyReady(p => p.LoginName) ?
                            LoadObject(prin) : prin;

            CTX.Lae(def);

            Principal  = res;
            Definition = def;
            Name       = res is User ?
                         ((User)res).Email : res.Title;
        }
 internal SPPermission(SPSecurable securable, RoleAssignment ass)
 {
     if (!ass.IsPropertyReady(a => a.Member.Title))
     {
         CTX.Lae(ass, true, a => a.Member.Title, a => a.Member.PrincipalType,
                 a => a.Member.LoginName, a => a.Member.Id, a => a.RoleDefinitionBindings);
     }
     _memTit         = ass.Member.Title;
     _memLog         = ass.Member.Id;
     _perms          = ParseBindings(ass.RoleDefinitionBindings);
     _prinType       = ass.Member.PrincipalType;
     this.LoginName  = ass.Member.LoginName;
     this.ObjectId   = securable.Id;
     this.ObjectName = securable.Name;
     _roleAss        = ass;
 }
Esempio n. 9
0
        public void AddPermission(SPBindingCollection bindingCol, bool forceBreak = false)
        {
            // This is the main "AddPermission" method that the other methods call.
            if (HasUniquePermissions.HasValue && !HasUniquePermissions.Value)
            {
                if (!forceBreak)
                {
                    throw new NoForceBreakException(_web.Id);
                }
                else
                {
                    _web.BreakRoleInheritance(true, true);
                }
            }
            else if (!HasUniquePermissions.HasValue)
            {
                throw new InvalidOperationException("This object's permissions cannot be modified!");
            }

            var list = new List <RoleAssignment>(bindingCol.Count);

            for (int i = 0; i < bindingCol.Count; i++)
            {
                var binding = bindingCol[i];
                var bCol    = new RoleDefinitionBindingCollection(CTX.SP1)
                {
                    binding.Definition
                };
                list.Add(_web.RoleAssignments.Add(
                             binding.Principal, bCol));
                foreach (var ass in list)
                {
                    CTX.Lae(ass, false);
                }
                _web.Update();
                CTX.Lae();
            }
            if (Permissions == null)
            {
                this.GetPermissions();
            }
            else
            {
                Permissions.AddRange(list);
            }
        }
Esempio n. 10
0
        private Principal LoadObject(Principal obj)
        {
            if (obj is User)
            {
                CTX.Lae((User)obj, true,
                        u => u.Email, u => u.LoginName,
                        u => u.Title, u => u.Id);
            }
            else
            {
                CTX.Lae(obj, true,
                        p => p.LoginName, p => p.Title,
                        p => p.Id);
            }

            return(obj);
        }
Esempio n. 11
0
        protected internal void Load <T>(T original, params string[] propertyNames) where T : ClientObject
        {
            var expressions = GetPropertyExpressionsNoType <T>(propertyNames).ToArray();

            CTX.Lae(original, true, expressions);
            var thisType = this.GetType();
            var thatType = typeof(T);

            for (int i = 0; i < propertyNames.Length; i++)
            {
                var prop     = propertyNames[i];
                var propInfo = thisType.GetProperty(prop);
                if (propInfo == null)
                {
                    if (allPropInfo == null)
                    {
                        allPropInfo = thisType.GetProperties();
                    }

                    for (int p = 0; p < allPropInfo.Length; p++)
                    {
                        var pi = allPropInfo[p];
                        if (string.Equals(pi.Name, prop, StringComparison.InvariantCultureIgnoreCase))
                        {
                            propInfo = pi;
                            break;
                        }
                    }
                    if (propInfo == null)
                    {
                        throw new ArgumentException(prop + " was not recognized as a valid property name for this object!");
                    }
                }
                var thatObj = thatType.InvokeMember(propInfo.Name, GETPROP, null, original, null);
                if (thatObj is ClientObject && ToSPType(thatObj.GetType(), out Type newType))
                {
                    MethodInfo GenericCast = this.GetType().GetMethod(
                        "Cast", NONPUBINST).MakeGenericMethod(newType);
                    thatObj = GenericCast.Invoke(this, new object[1] {
                        thatObj
                    });
                }
                propInfo.SetValue(this, thatObj, NONPUBINST,
                                  null, null, CultureInfo.CurrentCulture);
            }
        }
Esempio n. 12
0
        public void AddPermission(string principal, string roleDefinition, bool forceBreak = false)
        {
            var user = _web.EnsureUser(principal);

            CTX.Lae(user);
            if (CTX.AllRoles == null)
            {
                CTX.AllRoles = _web.RoleDefinitions;
                CTX.Lae(CTX.AllRoles, true,
                        ar => ar.Include(
                            r => r.Name
                            )
                        );
            }
            var roleDef = CTX.AllRoles.Single(r => string.Equals(r.Name, roleDefinition, StringComparison.OrdinalIgnoreCase));

            AddPermission(new SPBindingCollection(user, roleDef), forceBreak);
        }
Esempio n. 13
0
        public SPBinding(string principal, string roleDefinition)
        {
            var prin = CTX.SP1.Web.EnsureUser(principal);

            CTX.Lae(prin);
            var roleDefs = CTX.SP1.Web.RoleDefinitions;

            CTX.Lae(roleDefs, true, rds => rds.Include(
                        def => def.Name
                        ));
            var theRole = roleDefs.Single(x => string.Equals(roleDefinition, x.Name, StringComparison.OrdinalIgnoreCase));

            CTX.Lae(theRole);
            Principal  = prin;
            Definition = theRole;
            Name       = string.IsNullOrEmpty(prin.Email) ?
                         prin.Title : prin.Email;
        }
Esempio n. 14
0
        private static List FindRealListByName(string listName)
        {
            //_list
            if (listName.Contains("/") && !listName.StartsWith("/"))
            {
                listName = "/" + listName;
            }

            var allLists = CTX.SP1.Web.Lists;

            CTX.Lae(allLists, true, ls => ls.Include(
                        l => l.Title, l => l.RootFolder.ServerRelativeUrl
                        )
                    );
            return(allLists.Single(
                       l => l.Title.Equals(listName, StringComparison.InvariantCultureIgnoreCase) ||
                       l.RootFolder.ServerRelativeUrl.Equals(listName, StringComparison.InvariantCultureIgnoreCase)));
        }
        public static SPPermissionCollection ResolvePermissions(SPSecurable securable)
        {
            var roleAss = securable.SecObj.RoleAssignments;

            if (!roleAss.AreItemsAvailable)
            {
                CTX.Lae(roleAss, true, col => col.Include(
                            ass => ass.Member, ass => ass.RoleDefinitionBindings.Include(
                                d => d.Name, d => d.Description)));
            }

            var permCol = new SPPermissionCollection(roleAss.Count);

            foreach (var ass in roleAss)
            {
                permCol._list.Add(SPPermission.ResolvePermission(ass, securable));
            }
            return(permCol);
        }
        public bool ResetInheritance()
        {
            bool result = true;

            if (!HasUniquePermissions.HasValue || (HasUniquePermissions.HasValue && !HasUniquePermissions.Value))
            {
                throw new InvalidResetInheritanceException(this.Id);
            }

            SecObj.ResetRoleInheritance();
            try
            {
                CTX.Lae();
            }
            catch
            {
                result = false;
            }
            return(result);
        }
        public bool BreakInheritance(bool copyRoleAssignments, bool clearSubscopes)
        {
            bool result = true;

            if (HasUniquePermissions.HasValue && HasUniquePermissions.Value)
            {
                throw new InvalidBreakInheritanceException(this.Id);
            }

            SecObj.BreakRoleInheritance(copyRoleAssignments, clearSubscopes);
            try
            {
                CTX.Lae();
            }
            catch
            {
                result = false;
            }
            return(result);
        }
Esempio n. 18
0
 public SPListCollection GetLists(params string[] loadProperties)
 {
     if (loadProperties == null)
     {
         Lists = (SPListCollection)_web.Lists;
     }
     else
     {
         var allLists    = CTX.SP1.Web.Lists;
         var expressions = GetPropertyExpressions <List>(loadProperties).ToArray();
         CTX.Lae(allLists, true, al => al.Include(expressions));
         Lists = new SPListCollection();
         for (int i = 0; i < allLists.Count; i++)
         {
             var splist = (SPList)allLists[i];
             Lists.Add(splist);
         }
         Lists.IsReadOnly = true;
     }
     return(Lists);
 }
        public bool BreakInheritance(bool copyRoleAssignments, bool clearSubscopes = true)
        {
            bool result = true;

            if (HasUniquePermissions.HasValue && HasUniquePermissions.Value)
            {
                throw new InvalidBreakInheritanceException(_file.UniqueId);
            }

            _file.ListItemAllFields.BreakRoleInheritance(copyRoleAssignments, clearSubscopes);
            try
            {
                CTX.Lae();
                _hup = true;
            }
            catch
            {
                result = false;
            }
            return(result);
        }
        public bool ResetInheritance()
        {
            bool result = true;

            if (!HasUniquePermissions.HasValue || (HasUniquePermissions.HasValue && !HasUniquePermissions.Value))
            {
                throw new InvalidResetInheritanceException(_file.UniqueId);
            }

            _file.ListItemAllFields.ResetRoleInheritance();
            try
            {
                CTX.Lae();
                _hup = false;
            }
            catch
            {
                result = false;
            }
            return(result);
        }
        public SPSecurable(SecurableObject so)
        {
            SecObj = so;
            try
            {
                CTX.Lae(SecObj, true, s => s.HasUniqueRoleAssignments, s => s.RoleAssignments);
            }
            catch (ServerException ex)
            {
                if (ex.Message.Contains("does not belong to a list."))
                {
                    _pol = false;
                }
            }

            HasUniquePermissions = !SecObj.IsPropertyAvailable("HasUniqueRoleAssignments") ?
                                   null : (bool?)SecObj.HasUniqueRoleAssignments;

            if (!this.HasUniquePermissions.HasValue)
            {
                _pol = false;
            }
        }
        //public SPPermissionCollection GetUserPermissions(string userId)
        //{
        //    if (this.CanSetPermissions)
        //    {
        //        var user = new SPUser(userId, true);
        //        if (this.Permissions == null)
        //            this.GetPermissions();

        //        if (this.Permissions)
        //    }
        //    else
        //        return null;
        //}

        #endregion

        #region GET PERMISSIONS

        public SPPermissionCollection GetPermissions()
        {
            if (!SecObj.IsPropertyReady(x => x.RoleAssignments))
            {
                CTX.Lae(SecObj, true, s => s.RoleAssignments);
            }

            Type secType     = SecObj.GetType();
            var  genMeth     = ExpressionMethod.MakeGenericMethod(secType);
            var  expressions = genMeth.Invoke(this, new object[1] {
                new string[2] {
                    NameProperty, IdProperty
                }
            });

            var specLae = typeof(CTX).GetMethod("SpecialLae", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(SecObj.GetType());

            specLae.Invoke(null, new object[3] {
                SecObj, true, expressions
            });

            Permissions = SPPermissionCollection.ResolvePermissions(this);
            return(Permissions);
        }
 public static void LoadProperty <T>(this T clientObject, bool andExecute, params Expression <Func <T, object> >[] property)
     where T : ClientObject => CTX.Lae(clientObject, andExecute, property);