Ejemplo n.º 1
0
        public IHttpActionResult Create(AccountUserModel accountModel)
        {
            var account = accountModel.ToEntity();

            //问题: An entity object cannot be referenced by multiple instances of IEntityChangeTracker
            //状态:Fixed
            //原因:不明,但是应该和缓存机制有关
            var government = _governmentService.GetGovernmentUnitById(accountModel.GovernmentId);

            if (government == null)
            {
                return(BadRequest("用户所属单位不存在"));
            }
            account.Government = government;

            var registerRole = _accountService.GetAccountUserRoleBySystemName(SystemAccountUserRoleNames.Registered);
            var adminRole    = _accountService.GetAccountUserRoleBySystemName(SystemAccountUserRoleNames.Administrators);

            account.Password = "******";  //设置初始密码

            var role = _accountService.GetAccountUserRoleBySystemName(accountModel.RoleName);

            if (role != null && accountModel.RoleName != SystemAccountUserRoleNames.Registered)
            {
                account.AccountUserRoles.Add(role);
            }

            var registrationRequest = new AccountUserRegistrationRequest(account, account.UserName,
                                                                         account.Password, _accountUserSettings.DefaultPasswordFormat, accountModel.Active);

            var registrationResult = _accountUserRegistrationService.RegisterAccountUser(registrationRequest);

            if (registrationResult.Success)
            {
                //保存用户
                _accountService.InsertAccountUser(account);

                //activity log
                _accountUserActivityService.InsertActivity("AddNewAccount", "增加 名为 {0} 的用户", account.UserName);

                return(Ok(account.ToModel()));
            }
            else
            {
                return(BadRequest("添加用户失败"));
            }
        }
Ejemplo n.º 2
0
        public IHttpActionResult LoggingUser(string authCode = "")
        {
            if (string.IsNullOrEmpty(authCode))
            {
                return(BadRequest("AuthCode is invalid"));
            }

            var dingdingUserInfo = _ddTalkService.GetUserInfo(authCode);

            if (dingdingUserInfo.IsError)
            {
                return(BadRequest("Get UserInfo Error ,because " + dingdingUserInfo.ErrMsg));
            }

            var account = _accountUserService.GetAccountUserByDDUserId(dingdingUserInfo.Userid);

            if (account != null)
            {
                account.LastActivityDate = DateTime.Now;
                account.LastLoginDate    = DateTime.Now;
                account.LastIpAddress    = _webHelper.GetCurrentIpAddress();
                account.FirstTime        = false;
                _accountUserService.UpdateAccountUser(account);
            }
            else
            {
                account = new AccountUser
                {
                    FirstTime        = true,
                    Active           = true,
                    DDUserId         = dingdingUserInfo.Userid,
                    NickName         = dingdingUserInfo.Name,
                    LastActivityDate = DateTime.Now,
                    LastLoginDate    = DateTime.Now,
                    LastIpAddress    = _webHelper.GetCurrentIpAddress(),
                };
                _accountUserService.InsertAccountUser(account);
            }

            return(Ok("Guid:" + account.AccountUserGuid));
        }
Ejemplo n.º 3
0
        public IHttpActionResult SetRoles()
        {
            //  return BadRequest("角色配置 cloesd");
            #region 用户角色创建

            var crAdministrators = new AccountUserRole();
            var crRegistered     = new AccountUserRole();

            var roleNames = new List <string> {
                SystemAccountUserRoleNames.Administrators,
                SystemAccountUserRoleNames.DataReviewer,
                SystemAccountUserRoleNames.GovAuditor,
                SystemAccountUserRoleNames.StateOwnerAuditor,
                SystemAccountUserRoleNames.ParentGovernmentorAuditor,
                SystemAccountUserRoleNames.Registered
            };

            foreach (var roleName in roleNames)
            {
                var role = _accountUserService.GetAccountUserRoleBySystemName(roleName);
                if (role == null)
                {
                    role = new AccountUserRole
                    {
                        Name         = roleName,
                        Active       = true,
                        IsSystemRole = true,
                        SystemName   = roleName
                    };

                    _accountUserService.InsertAccountUserRole(role);
                }
                if (roleName == SystemAccountUserRoleNames.Administrators)
                {
                    crAdministrators = role;
                }
                if (roleName == SystemAccountUserRoleNames.Registered)
                {
                    crRegistered = role;
                }
            }
            #endregion
            #region 测试组织机构

            var cz = new GovernmentUnit
            {
                Name           = "县财政局",
                GovernmentType = GovernmentType.Government,
                Person         = "联系人",
                Tel            = "0570-5062456"
            };
            _governmentService.InsertGovernmentUnit(cz);

            #endregion

            #region 用户创建

            var user = new AccountUser()
            {
                UserName         = "******",
                AccountUserGuid  = Guid.NewGuid(),
                Active           = true,
                CreatedOn        = DateTime.Now,
                IsSystemAccount  = false,
                Password         = "******",
                PasswordFormat   = PasswordFormat.Clear,
                LastActivityDate = DateTime.Now,
                Deleted          = false,
                UpdatedOn        = DateTime.Now,
                Government       = cz
            };
            user.AccountUserRoles.Add(crAdministrators);
            user.AccountUserRoles.Add(crRegistered);
            _accountUserService.InsertAccountUser(user);

            user = new AccountUser()
            {
                UserName         = "******",
                AccountUserGuid  = Guid.NewGuid(),
                Active           = true,
                CreatedOn        = DateTime.Now,
                IsSystemAccount  = false,
                Password         = "******",
                PasswordFormat   = PasswordFormat.Clear,
                LastActivityDate = DateTime.Now,
                Deleted          = false,
                UpdatedOn        = DateTime.Now,
                Government       = cz
            };
            _accountUserService.InsertAccountUser(user);

            #endregion

            return(Ok("角色配置完成"));
        }