コード例 #1
0
        public void InsertUserRole()
        {
            IdentityUser user = new IdentityUser {
                UserName = "******", Email = "*****@*****.**", PhoneNumber = "1234567890", PasswordHash = "@#$rfgt$##WE", EmailConfirmed = true, PhoneNumberConfirmed = true, TwoFactorEnabled = false, LockoutEnabled = false, AccessFailedCount = 0
            };

            user = userTable.Insert(user);
            dynamic role = new { Name = "Admin" };

            role = roleTable.Insert(role);
            object userRole = new { UserId = user.Id, RoleId = role.Id };

            userRole = userRoleTable.Insert(userRole);
            int count = userRoleTable.All($"UserId={user.Id} and RoleId={role.Id}").ToList().Count;

            Assert.AreEqual(1, count);
        }
コード例 #2
0
        // ----- User-Role funcionality ---------------------------------------

        /// <summary>
        /// Add a role by ist name: Check if a global or client specific role exists
        /// (take user.ClientId to specify) --> assign it ;-) If not: Take a possibly
        /// existing global role.

        /// </summary>
        /// <param name="user"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public Task AddToRoleAsync(TUser user, string roleName)
        {
            if (user == null)
            {
                throw new ArgumentNullException(IdentityConstants.User);
            }

            var rolesFound = _roleTable.GetRoles().Where(r => r.Name == roleName).ToList();

            if (!rolesFound.Any())
            {
                throw new ArgumentNullException(IdentityConstants.Role);
            }

            var specificRole = rolesFound.FirstOrDefault(r => r.ClientId == user.ClientId);
            var globalRole   = rolesFound.FirstOrDefault(r => r.ClientId == null);
            var roleToAdd    = specificRole ?? globalRole;

            _userRoleTable.Insert(user, roleToAdd.Id);

            return(Task.FromResult <object>(null));
        }
コード例 #3
0
        public void It_creates_and_inserts_new_userrole()
        {
            IdentityRole userRole;
            int          result;

            using (var transaction = _database.GetTransaction())
            {
                var user = _userTable.GetUserByName(name1).FirstOrDefault();
                var role = _roleTable.GetRoleByName(role1);
                result   = _userRoleTable.Insert(user.Id, role.Id);
                userRole = _userRoleTable.GetRoles(user.Id).FirstOrDefault();
                transaction.Dispose();
            }

            Assert.AreEqual(1, result);
            Assert.AreEqual(role1, userRole.Name);
        }
コード例 #4
0
 private static int CreateUserRole(IdentityUser user, string role)
 {
     return(_userRoleTable.Insert(user, _roleTable.GetRoleId(role)));
 }