예제 #1
0
        /// <summary>
        /// Updates user's data in the database
        /// </summary>
        /// <param name="id">User ID</param>
        /// <param name="userVm">Input user's data</param>
        public void UpdateUser(string id, TbUserRoleVm userVm)
        {
            if(id != userVm.UserId)
            {
                throw new Exception("User ID conflict.");
            }

            var user = UserManager.FindById(userVm.UserId);
            if(user == null)
            {
                throw new Exception(string.Format("Unable to find user '{0}'.", userVm.UserName));
            }

            // Execute mapping from the view model to the domain object
            user = _mapper.Map<TbUserRoleVm, TbUser>(userVm, user);

            // Envelop the sequence of the db operations in the transaction scope
            using(var transaction = TransactionProvider.GetTransactionScope())
            {
                // Update user's data
                var result = UserManager.Update(user);
                if(result == null || !result.Succeeded)
                {
                    throw new TbIdentityException("Update user error", result);
                }
            
                // Change user's role if it had been changed
                string prevRole = UserManager.GetRoles(id).FirstOrDefault();
                if(prevRole != userVm.Role)
                {
                    result = UserManager.RemoveFromRole(id, prevRole);
                    if(result == null || !result.Succeeded)
                    {
                        throw new TbIdentityException("Remove from role error", result);
                    }
                    result = UserManager.AddToRole(id, userVm.Role);
                    if(result == null || !result.Succeeded)
                    {
                        throw new TbIdentityException("Add to role error", result);
                    }
                }
                transaction.Complete();
            }
        }
예제 #2
0
        /// <summary>
        /// Adds a user to the system asyncronously
        /// </summary>
        /// <param name="userModel">User's data</param>
        /// <returns>Task to enable asynchronous execution</returns>
        public async Task AddUserAsync(TbUserRoleVm userModel)
        {
            // Base user constructor creates Id, which must be kept in the model before mapping
            var user = new TbUser();
            userModel.UserId = user.Id;

            // Execute mapping from the view model to the domain object
            user = _mapper.Map<TbUserRoleVm, TbUser>(userModel, user);

            // TODO: consider more secure psw generation 
            string password = "******";

            // Envelop the sequence of the db operations in the transaction scope
            using(var transaction = TransactionProvider.GetTransactionScope())
            {
                // Create a user
                var result = UserManager.Create(user, password);
                if(result == null || !result.Succeeded)
                {
                    throw new TbIdentityException("Create user error", result);
                }

                // Add a user to the specified role
                string role = userModel.Role;
                long projectId = (long)userModel.ProjectId;
                string userId = user.Id;

                result = UserManager.AddToRole(userId, role);
                if(result == null || !result.Succeeded)
                {
                    throw new TbIdentityException("Add to role error", result);
                }

                // Add a user to the specified project
                var projectUsers = new ProjectUsers()
                {
                    ProjectId = projectId,
                    UserId = userId
                };

                var projectUsersRepository = _unitOfWork.ProjectUsersRepository;
                projectUsersRepository.Add(projectUsers);

                // If just added user is a manager, delete "NotAssigned" (manager) system account from the project
                if(role == RoleKey.Manager)
                {
                    string notAssignedUserId = UserManager.FindByName("NotAssigned").Id;
                    projectUsersRepository.DeleteByPredicate(x => x.UserId == notAssignedUserId && x.ProjectId == projectId);
                }
                _unitOfWork.Commit();
                transaction.Complete();
            }

            // Create email notification and send it
            string login = string.Format("{0}/#/login", Host);
            string retrive = string.Format("{0}/#/forgotPassword", Host);
            string body = string.Format(_addUserEmailBodyConst, user.FirstName, user.UserName, password, retrive, login);
            MailMessage message = new MailMessage();
            message.To.Add(user.Email);
            message.Subject = "Add account";
            message.Body = body;

            await _emailService.SendMailAsync(message);
        }