예제 #1
0
        public async Task <IdentityResult> CreateAsync(ApplicationRole role, CancellationToken cancellationToken)
        {
            if (role == null)
            {
                throw new ArgumentNullException(nameof(role));
            }

            if (!Constants.RoleName.GetAllValues <string>().Contains(role.Name))
            {
                return(IdentityResult.Failed(_identityErrorDescriber.InvalidRoleName(role.Name)));
            }

            var roleEntity = new IdentityRoleEntity {
                Name = role.Name
            };

            try
            {
                if (await _appDb.RoleRepository.RoleNameExistsAsync(role.Name, cancellationToken))
                {
                    return(IdentityResult.Failed(_identityErrorDescriber.DuplicateRoleName(role.Name)));
                }

                await _appDb.GenericRepository.SaveEntityAsync(roleEntity, true, false, cancellationToken);

                role.Id = roleEntity.Id;
            }
            catch (Exception)
            {
                _logger.LogError($"Role name '{role.Name}' could not be created");
                return(IdentityResult.Failed(_identityErrorDescriber.DefaultError()));
            }

            return(IdentityResult.Success);
        }
        /// <summary>	Finds the roles in this collection. </summary>
        /// <param name="role">	The role. </param>
        /// <returns>
        ///     An enumerator that allows foreach to be used to process the roles in this collection.
        /// </returns>
        public virtual IEnumerable <int> FindByRole(IdentityRoleEntity role)
        {
            var command = SqlBuilder.SelectByFilter(EntityType, nameof(IdentityUserRoleEntity.RoleId),
                                                    new[] { nameof(IdentityUserRoleEntity.UserId) });

            return(UnitOfWork.Connection.Query <int>(command, new { RoleId = role.Id },
                                                     UnitOfWork.Transaction));
        }
예제 #3
0
 /// <summary>	Sets role name asynchronous. </summary>
 /// <param name="role">					The role. </param>
 /// <param name="roleName">				Name of the role. </param>
 /// <param name="cancellationToken">	The cancellation token. </param>
 /// <returns>	A Task. </returns>
 public Task SetRoleNameAsync(IdentityRoleEntity role, string roleName, CancellationToken cancellationToken)
 {
     return(Task.Factory.StartNew(() =>
     {
         role.Name = roleName;
         UnitOfWork.RoleRepository.Update(role);
     }, cancellationToken));
 }
        /// <summary>Can delete activity.</summary>
        public virtual void CanDeleteActivityRole()
        {
            var role = new IdentityRoleEntity
            {
                ApplicationId  = 0,
                Description    = "MyRole",
                Identifier     = Guid.NewGuid(),
                NormalizedName = "myrole",
                Name           = "MyRole"
            };

            using (var iUow = _identityDataService.StartUnitOfWork())
            {
                role = iUow.RoleRepository.Add(role);
                iUow.Commit();
            }

            try
            {
                using (var uow = DataService.StartUnitOfWork())
                {
                    var activity = new ActivityEntity
                    {
                        Name                = "MyActivity",
                        ResourceName        = "MyResource",
                        ResourceDisplayName = "MyDisplayName",
                        GroupName           = "MyGroupName",
                        GroupDisplayName    = "MyGroupDisplayName"
                    };
                    activity = uow.ActivityRepository.Add(activity);

                    var aRole = new ActivityRoleEntity
                    {
                        ActivityId = activity.Id,
                        RoleId     = role.Id,
                        Allow      = true
                    };

                    uow.ActivityRoleRepository.Add(aRole);

                    aRole.Allow = false;
                    uow.ActivityRoleRepository.Delete(aRole);

                    Assert.AreEqual(null, uow.ActivityRoleRepository.Get(aRole.Id));
                }
            }
            finally
            {
                using (var iUow = _identityDataService.StartUnitOfWork())
                {
                    iUow.RoleRepository.Delete(role);
                    iUow.Commit();
                }
            }
        }
예제 #5
0
        public virtual void CanUpdateUserRole()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var user = new IdentityUserEntity
                {
                    Identifier        = Guid.NewGuid(),
                    Name              = "Achim Schnell",
                    NormalizedName    = "ACHIM SCHNELL",
                    Email             = "*****@*****.**",
                    NormalizedEmail   = "*****@*****.**",
                    AccessFailedCount = 0,
                    ApplicationId     = 0,
                    EmailConfirmed    = true,
                    IsAnonymous       = false,
                    LastActivityDate  = DateTime.Now
                };
                uow.UserRepository.Add(user);
                var user2 = new IdentityUserEntity
                {
                    Identifier        = Guid.NewGuid(),
                    Name              = "Stefan Schnell",
                    NormalizedName    = "STEFAN SCHNELL",
                    Email             = "*****@*****.**",
                    NormalizedEmail   = "*****@*****.**",
                    AccessFailedCount = 0,
                    ApplicationId     = 0,
                    EmailConfirmed    = true,
                    IsAnonymous       = false,
                    LastActivityDate  = DateTime.Now
                };
                uow.UserRepository.Add(user2);

                var role = new IdentityRoleEntity
                {
                    Identifier     = Guid.NewGuid(),
                    Name           = "DummyRole",
                    ApplicationId  = 0,
                    Description    = "DummyRole",
                    NormalizedName = "dummyrole"
                };
                uow.RoleRepository.Add(role);

                var userRole = new IdentityUserRoleEntity
                {
                    UserId = user.Id,
                    RoleId = role.Id
                };
                userRole        = uow.UserRoleRepository.Add(userRole);
                userRole.UserId = user2.Id;
                uow.UserRoleRepository.Update(userRole);

                Assert.AreEqual(userRole.UserId, uow.UserRoleRepository.Get(userRole.Id).UserId);
            }
        }
예제 #6
0
 /// <summary>	Deletes the asynchronous. </summary>
 /// <param name="role">					The role. </param>
 /// <param name="cancellationToken">	The cancellation token. </param>
 /// <returns>	A Task&lt;IdentityResult&gt; </returns>
 public Task <IdentityResult> DeleteAsync(IdentityRoleEntity role, CancellationToken cancellationToken)
 {
     return(Task <IdentityResult> .Factory.StartNew(() =>
     {
         try
         {
             UnitOfWork.RoleRepository.Delete(role);
             return IdentityResult.Success;
         }
         catch (Exception)
         {
             return IdentityResult.Failed();
         }
     }, cancellationToken));
 }
        public virtual void CanAddAndFindByIds()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var role = new IdentityRoleEntity
                {
                    Identifier     = Guid.NewGuid(),
                    Name           = "DummyRole",
                    ApplicationId  = 0,
                    Description    = "DummyRole",
                    NormalizedName = "dummyrole"
                };

                uow.RoleRepository.Add(role);
                Assert.AreEqual(1, uow.RoleRepository.FindByIds(new[] { role.Id }).Count());
            }
        }
        /// <summary>	Can add and get role. </summary>
        public virtual void CanAddAndGetRoleByIdentifier()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var role = new IdentityRoleEntity
                {
                    Identifier     = Guid.NewGuid(),
                    Name           = "DummyRole",
                    ApplicationId  = 0,
                    Description    = "DummyRole",
                    NormalizedName = "dummyrole"
                };

                uow.RoleRepository.Add(role);
                Assert.AreEqual(role.Name, uow.RoleRepository.Get(role.Identifier.ToString()).Name);
            }
        }
        public IActionResult AddRole(RoleAddModel model)
        {
            if (ModelState.IsValid)
            {
                using (var uow = _identityDataService.StartUnitOfWork())
                {
                    var role = new IdentityRoleEntity
                    {
                        ApplicationId  = 0,
                        Name           = model.Name,
                        Description    = model.Description,
                        Identifier     = Guid.NewGuid(),
                        NormalizedName = model.Name.ToUpper()
                    };
                    uow.RoleRepository.Add(role);
                    uow.Commit();
                    return(RedirectToAction(nameof(ManageRoles)));
                }
            }

            return(View(model));
        }
예제 #10
0
 /// <summary>	Gets normalized role name asynchronous. </summary>
 /// <param name="role">					The role. </param>
 /// <param name="cancellationToken">	The cancellation token. </param>
 /// <returns>	The normalized role name asynchronous. </returns>
 public Task <string> GetNormalizedRoleNameAsync(IdentityRoleEntity role, CancellationToken cancellationToken)
 {
     return(Task.FromResult(role.NormalizedName));
 }
예제 #11
0
 /// <summary>	Gets role identifier asynchronous. </summary>
 /// <param name="role">					The role. </param>
 /// <param name="cancellationToken">	The cancellation token. </param>
 /// <returns>	The role identifier asynchronous. </returns>
 public Task <string> GetRoleIdAsync(IdentityRoleEntity role, CancellationToken cancellationToken)
 {
     return(Task.FromResult(role.Identifier.ToString()));
 }
        /// <summary>Can add and get activities.</summary>
        public virtual void CanAddAndGetActivityRoles()
        {
            var activity1 = new ActivityEntity
            {
                Name                = "MyActivity 1",
                ResourceName        = "MyResource",
                ResourceDisplayName = "MyDisplayName",
                GroupName           = "MyGroupName",
                GroupDisplayName    = "MyGroupDisplayName"
            };
            var activity2 = new ActivityEntity
            {
                Name                = "MyActivity 2",
                ResourceName        = "MyResource",
                ResourceDisplayName = "MyDisplayName",
                GroupName           = "MyGroupName",
                GroupDisplayName    = "MyGroupDisplayName"
            };

            var role = new IdentityRoleEntity
            {
                ApplicationId  = 0,
                Description    = "MyRole",
                Identifier     = Guid.NewGuid(),
                NormalizedName = "myrole",
                Name           = "MyRole"
            };

            using (var iUow = _identityDataService.StartUnitOfWork())
            {
                role = iUow.RoleRepository.Add(role);
                iUow.Commit();
            }

            try
            {
                using (var uow = DataService.StartUnitOfWork())
                {
                    activity1 = uow.ActivityRepository.Add(activity1);
                    activity2 = uow.ActivityRepository.Add(activity2);

                    var aRoles = new[]
                    {
                        new ActivityRoleEntity
                        {
                            ActivityId = activity1.Id,
                            RoleId     = role.Id,
                            Allow      = true
                        },
                        new ActivityRoleEntity
                        {
                            ActivityId = activity2.Id,
                            RoleId     = role.Id,
                            Allow      = true
                        }
                    };

                    uow.ActivityRoleRepository.AddRange(aRoles);
                    Assert.AreEqual(aRoles.Length, uow.ActivityRoleRepository.GetAll().Count());
                }
            }
            finally
            {
                using (var iUow = _identityDataService.StartUnitOfWork())
                {
                    iUow.RoleRepository.Delete(role);
                    iUow.Commit();
                }
            }
        }
 /// <summary>
 ///     Finds the roles in this collection.
 /// </summary>
 /// <param name="role"> The role. </param>
 /// <returns>
 ///     An enumerator that allows foreach to be used to process the roles in this collection.
 /// </returns>
 public IEnumerable <int> FindByRole(IdentityRoleEntity role)
 {
     return(Collection.Find(e => e.RoleId == role.Id).Select(e => e.UserId));
 }