コード例 #1
0
        public void AddPermissionLevelToUserTest()
        {
            using (ClientContext clientContext = TestCommon.CreateClientContext())
            {
                Web web = clientContext.Web;

                var roleType = RoleType.Contributor;

                //Setup: Make sure permission does not already exist
                web.RemovePermissionLevelFromUser(_userLogin, roleType);

                //Add Permission
                web.AddPermissionLevelToUser(_userLogin, roleType);

                //Get User
                User user = web.EnsureUser(_userLogin);
                clientContext.ExecuteQueryRetry();

                //Assert
                Assert.IsTrue(CheckPermissionOnPrinciple(web, user, roleType));

                //Teardown: Expicitly remove given permission.
                web.RemovePermissionLevelFromUser(_userLogin, roleType);
            }
        }
コード例 #2
0
ファイル: SecurityExtensionsTests.cs プロジェクト: vcx/PnP
        public void AddPermissionLevelToUserTest()
        {
            using (var clientContext = TestCommon.CreateClientContext())
            {
                Web web = clientContext.Web;

                RoleType roleType = RoleType.Contributor;

                //Setup: Make sure permission does not already exist
                web.RemovePermissionLevelFromUser(_userLogin, roleType);

                //Add Permission
                web.AddPermissionLevelToUser(_userLogin, roleType);

                //Get User
                User user = web.SiteUsers.GetByEmail(_userLogin);
                clientContext.Load(user);
                clientContext.ExecuteQuery();

                //Get Roles for the User
                RoleDefinitionBindingCollection roleDefinitionBindingCollection = web.RoleAssignments.GetByPrincipal(user).RoleDefinitionBindings;
                clientContext.Load(roleDefinitionBindingCollection);
                clientContext.ExecuteQuery();

                //Check if assigned role is found
                bool roleExists = false;
                foreach (RoleDefinition rd in roleDefinitionBindingCollection)
                {
                    if (rd.RoleTypeKind == roleType)
                    {
                        roleExists = true;
                    }
                }

                //Assert
                Assert.IsTrue(roleExists);

                //Teardown: Expicitly remove given permission.
                web.RemovePermissionLevelFromUser(_userLogin, roleType);
            }
        }