Beispiel #1
0
        /// <summary>   Gets the activities in this collection. </summary>
        /// <param name="user">             The user. </param>
        /// <param name="resourceTypeName"> Name of the resource type. </param>
        /// <param name="operationName">    Name of the operation. </param>
        /// <returns>
        ///     An enumerator that allows foreach to be used to process the activities in this collection.
        /// </returns>
        private IEnumerable <ActivityRoleEntity> GetActivities(ClaimsPrincipal user, string resourceTypeName,
                                                               string operationName)
        {
            if (_activities.ContainsKey(user))
            {
                return(_activities[user]);
            }

            using (var uow = _dataService.StartUnitOfWork())
            {
                var activity      = uow.ActivityRepository.GetByResourceAndActivity(resourceTypeName, operationName);
                var activityRoles = activity == null
                    ? Enumerable.Empty <ActivityRoleEntity>()
                    : uow.ActivityRoleRepository.ByActivity(activity).ToList();

                if (activityRoles != null)
                {
                    using (var iuow = _identityDataService.StartUnitOfWork())
                    {
                        var userRoleNames = user.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value)
                                            .ToList();
                        var roleIds = iuow.RoleRepository.FindByNames(userRoleNames).Select(r => r.Id);
                        activityRoles = activityRoles.Where(ar => roleIds.Contains(ar.RoleId)).ToList();
                    }
                }

                _activities[user] = activityRoles;
            }

            return(_activities[user]);
        }
        /// <summary>Can add and get activity.</summary>
        public virtual void CanAddAndGetActivityRole()
        {
            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);

                    Assert.AreEqual(aRole.Id, uow.ActivityRoleRepository.Get(aRole.Id).Id);
                }
            }
            finally
            {
                using (var iUow = _identityDataService.StartUnitOfWork())
                {
                    iUow.RoleRepository.Delete(role);
                    iUow.Commit();
                }
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                using (var uow = _identityDataService.StartUnitOfWork())
                {
                    if (uow.UserRepository.Count() == 0)
                    {
                        var admin = await RegisterFirstUserAdminWithoutConfirmation(uow, model, returnUrl);

                        if (admin == null)
                        {
                            return(View(model));
                        }
                        await _signInManager.SignInAsync(admin, false);

                        return(RedirectToLocal(returnUrl));
                    }
                }

                var user = new IdentityUserEntity {
                    Name = model.Name, Email = model.Email, Phone = model.Phone
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SendConfirmationMail(user);

                    // sign the user in
                    // disabled to force the the user to confirm his mail address
                    //await _signInManager.SignInAsync(user, isPersistent: false);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToAction(nameof(ConfirmEmailNotification)));
                }

                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #4
0
 /// <summary>Constructor.</summary>
 /// <exception cref="ArgumentNullException">    Thrown when one or more required arguments are
 ///                                             null. </exception>
 /// <param name="dataService">  The data service. </param>
 /// <param name="describer">    The describer. </param>
 public IdentityStore(IIdentityDataService dataService, IdentityErrorDescriber describer)
 {
     UnitOfWork     = dataService?.StartUnitOfWork() ?? throw new ArgumentNullException(nameof(dataService));
     ErrorDescriber = describer;
 }
 public IActionResult ManageUsers()
 {
     using (var uow = _identityDataService.StartUnitOfWork())
     {
         var users = uow.UserRepository.GetAll();
         return(View(users));
     }
 }