예제 #1
0
        public object Any(AllUserInfo request)
        {
            var sessionKeys = Cache.GetKeysStartingWith(IdUtils.CreateUrn <IAuthSession>("")).ToList();
            var allSessions = Cache.GetAll <IAuthSession>(sessionKeys);

            return(new AllUserInfoResponse {
                UserSessions = allSessions.Values.Map(x => (CustomUserSession)x),
                AppUsers = Db.Select <AppUser>(),
                UserAuthDetails = Db.Select <UserAuthDetails>(),
            });
        }
        public async Task <ActionResult <User> > Post(AllUserInfo NewUser)
        {
            Users user = new Users
            {
                Username        = NewUser.Username,
                FirstName       = NewUser.FirstName,
                LastName        = NewUser.LastName,
                DateOfBirth     = NewUser.DateOfBirth,
                UserAddress     = NewUser.UserAddress,
                PhoneNumber     = NewUser.PhoneNumber,
                Email           = NewUser.Email,
                LevelPermission = false,
                UserPic         = NewUser.UserPic
            };
            await Repo.AddUserAsync(user);

            return(NoContent());
        }
예제 #3
0
        /// <summary>
        /// 加载缓存
        /// </summary>
        /// <returns></returns>
        public void LoadCache(UserInfo info)
        {
            Dictionary <string, string> functionDict = new Dictionary <string, string>();
            List <FunctionInfo>         list         = BLLFactory <Function> .Instance.GetFunctionsByUser(info.Id, Portal.gc.SYSTEMTYPEID);

            if (list != null && list.Count > 0)
            {
                functionDict.Clear();
                foreach (FunctionInfo functionInfo in list)
                {
                    // 20200303 改用功能确认是否有权限
                    if (!functionDict.ContainsKey(functionInfo.DllPath))
                    {
                        functionDict.Add(functionInfo.DllPath, functionInfo.Name);
                    }
                }
            }

            #region 获取角色对应的用户操作部门及公司范围
            List <int> companyLst = BLLFactory <RoleData> .Instance.GetBelongCompanysByUser(info.Id);

            List <int> deptLst = BLLFactory <RoleData> .Instance.GetBelongDeptsByUser(info.Id);

            StringBuilder companysb = new StringBuilder();
            StringBuilder deptsb    = new StringBuilder();
            companysb.Append(" in (");
            for (int i = 0; i < companyLst.Count; i++)
            {
                companysb.Append(" '" + companyLst[i] + "', ");
            }
            companysb.Append(" '')");

            if (companyLst.Contains(-1))
            {
                companysb.Append(" or (1 = 1)");
            }

            deptsb.Append(" in (");
            for (int i = 0; i < deptLst.Count; i++)
            {
                deptsb.Append(" '" + deptsb[i] + "', ");
            }
            deptsb.Append(" '')");

            if (deptLst.Contains(-11))
            {
                deptsb.Append(" or (1 = 1)");
            }
            #endregion

            #region 获取标准字段
            XmlHelper   xmlhelper  = new XmlHelper(@"XML\stdfield.xml");
            XmlNodeList xmlNodeLst = xmlhelper.Read("datatype/dataitem");
            Dictionary <string, string> dicStdField = new Dictionary <string, string>();
            foreach (XmlNode xn1 in xmlNodeLst)
            {
                // 将节点转换为元素,便于得到节点的属性值
                XmlElement xe = (XmlElement)xn1;

                // 得到DataTypeInfo节点的所有子节点
                XmlNodeList xnl0 = xe.ChildNodes;

                dicStdField.Add(xnl0.Item(0).InnerText, xnl0.Item(1).InnerText);
            }
            #endregion

            #region 获取全部用户信息
            List <UserInfo> lst = BLLFactory <User> .Instance.GetAll();

            if (AllUserInfo == null)
            {
                AllUserInfo = new Dictionary <int, string>();
            }

            foreach (var user in lst)
            {
                if (!AllUserInfo.ContainsKey(user.Id))
                {
                    AllUserInfo.Add(user.Id, user.Name);
                }
            }
            #endregion

            #region 获取全部用户信息
            List <OUInfo> oulst = BLLFactory <OU> .Instance.GetAll();

            if (AllOuInfo == null)
            {
                AllOuInfo = new Dictionary <int, string>();
            }

            foreach (var ou in oulst)
            {
                if (!AllOuInfo.ContainsKey(ou.Id))
                {
                    AllOuInfo.Add(ou.Id, ou.Name);
                }
            }
            #endregion

            // 并保持到缓存中
            Cache.Instance["LoginUserInfo"] = ConvertToLoginUser(info);
            Cache.Instance["FunctionDict"]  = functionDict;
            Cache.Instance["RoleList"]      = BLLFactory <Role> .Instance.GetRolesByUser(info.Id);

            Cache.Instance["canOptCompanyId"] = companysb.ToString();
            Cache.Instance["canOptDeptId"]    = deptsb.ToString();
            Cache.Instance["DictData"]        = BLLFactory <DictData> .Instance.GetAllDict();

            Cache.Instance["AppConfig"] = Portal.gc.config;
        }
        public async Task <ActionResult> Register(AllUserInfo input,
                                                  [FromServices] UserManager <IdentityUser> userManager,
                                                  [FromServices] RoleManager <IdentityRole> roleManager)
        {
            // with an [ApiController], model state is always automatically checked
            // and return 400 if any errors.
            bool admin         = input.IsAdmin;
            bool UsernameTaken = Repo.CheckIfUsernameInDatabase(input.Username);

            if (UsernameTaken)
            {
                return(StatusCode(418, UsernameTaken));
            }

            log.Info("Beginning new user registration");
            var user = new IdentityUser(input.Username);

            var result = await userManager.CreateAsync(user, input.Password);

            if (!result.Succeeded)
            {
                log.Info("HTTP status code 400 - displaying error view");
                return(BadRequest(result));
            }

            log.Info("HTTP status code 200 - continuing with login");
            if (admin)
            {
                log.Info("User is adminiatrator");
                if (!(await roleManager.RoleExistsAsync("admin")))
                {
                    log.Info("Creating admin role");
                    var adminRole = new IdentityRole("admin");
                    result = await roleManager.CreateAsync(adminRole);

                    if (!result.Succeeded)
                    {
                        log.Info("Error: internal server error. Displaying result");
                        return(StatusCode(500, result));
                    }
                }
                log.Info("Administration role exists");
                log.Info("Adding admin role to user");
                result = await userManager.AddToRoleAsync(user, "admin");

                if (!result.Succeeded)
                {
                    log.Info("Error: internal server error. Displaying result");
                    return(StatusCode(500, result));
                }
            }

            log.Info("Logging in user");
            await _signInManager.SignInAsync(user, isPersistent : false);

            log.Info("Creating user for non-identity database");

            var permission = (input.IsAdmin);

            Users u = new Users
            {
                Username        = input.Username,
                FirstName       = input.FirstName,
                LastName        = input.LastName,
                DateOfBirth     = input.DateOfBirth,
                UserAddress     = input.UserAddress,
                PhoneNumber     = input.PhoneNumber,
                Email           = input.Email,
                LevelPermission = permission,
                UserPic         = input.UserPic
            };
            await Repo.AddUserAsync(u);

            log.Info("User registration successful");
            return(NoContent());
        }