/// <summary>
        /// 获取UMC用户列表
        /// </summary>
        /// <param name="ErrorMsg">如果失败,返回的错误信息</param>
        /// <returns>返回用户列表,如果失败返回null</returns>
        public static ReturnMessage GetAllUsers()
        {
            ReturnMessage msg = new ReturnMessage
            {
                Succeed = true
            };

            try
            {
                StringContent RequestBody = new StringContent("", Encoding.UTF8, "application/json"); //赋值
                ReturnMessage msg1        = SendJsonData(GetAllUsersURI, RequestBody, "usersresult"); //发送数据
                if (!msg1.Succeed)
                {
                    msg.Message = "获取用户失败," + msg1.Message;
                    return(msg);
                }

                JObject parsedJson = JObject.Parse(msg1.Result.ToString());
                JToken  res_Users;
                if ((parsedJson.TryGetValue("users", out res_Users))) //找到用户列表
                {
                    msg.Result  = JsonConvert.DeserializeObject(res_Users.ToString(), typeof(List <UMC_UserInfo>));
                    msg.Succeed = true;
                    return(msg);
                }
                else
                {
                    msg.Message = "返回的信息中未找到用户列表,返回内容为:" + msg1.Message;
                    return(msg);
                }
            }
            catch (System.Exception ex)
            {
                msg.Message = ex.Message;
            }

            return(msg);
        }
        //根据用户名启用或停用用户 --比直接用用户ID修改效率低很多(需要先去获取所有UMC用户,再根据用户名搜索用户ID,再去用用户ID修改信息)
        private static ReturnMessage SetUserEnabled(string UserName, bool isEnabled)
        {
            ReturnMessage msg = new ReturnMessage {
                Succeed = false
            };

            try
            {
                //UMC_UserInfo uinfo = GetUserBaseInfo(UserName, out ErrorMsg); //根据用户名获取用户信息
                //if (uinfo == null)
                //{
                //    ErrorMsg = "获取用户信息出错," + ErrorMsg;
                //    return false;
                //}

                //return EditUser(uinfo.ID, UserName, "", uinfo.FullName, isEnabled, out ErrorMsg); //调用修改方法
                return(msg);
            }
            catch (System.Exception ex)
            {
                msg.Message = ex.Message;
                return(msg);
            }
        }
        /// <summary>
        /// 根据UMC用户ID修改用户信息
        /// </summary>
        /// <param name="UserID">UMC的用户ID</param>
        /// <param name="UserID">UMC的用户名</param>
        /// <param name="Password">密码,如果不需要修改密码,则传入""</param>
        /// <param name="FullName">全名</param>
        /// <param name="Enabled">是否启用</param>
        /// <param name="CanChange">是否可以修改密码</param>
        /// <param name="MustChange">是否首次登录必须修改密码</param>
        /// <param name="ErrorMsg">如果失败,返回的错误信息</param>
        /// <returns>返回成功或失败</returns>
        private static ReturnMessage EditUser(int UserID, string UserName, string Password, string FullName, bool Enabled, bool CanChange, bool MustChange)
        {
            ReturnMessage msg = new ReturnMessage
            {
                Succeed = true
            };

            try
            {
                var user = new
                {
                    id         = UserID,
                    name       = UserName,
                    password   = Password,
                    fullname   = FullName,
                    enabled    = Enabled,
                    canchange  = CanChange,
                    mustchange = MustChange,
                    locked     = false
                };
                string strUser = JsonConvert.SerializeObject(user);

                StringContent RequestBody = new StringContent("[" + strUser + "]", Encoding.UTF8, "application/json"); //赋值
                ReturnMessage msg1        = SendJsonData(EditUserURI, RequestBody, "userupdateresult");                //发送数据
                if (!msg1.Succeed)
                {
                    msg.Message = "编辑出错," + msg1.Message;
                    return(msg);
                }

                JObject parsedJson = JObject.Parse(msg1.Result.ToString());
                JToken  res_Users;
                if ((parsedJson.TryGetValue("users", out res_Users))) //找到修改用户信息
                {
                    JToken fUser    = res_Users.First;                //取第一个用户
                    int    res_code = fUser.Value <int>("result");    //取创建状态
                    if (res_code == 0)                                //成功
                    {
                        msg.Succeed = true;
                        return(msg);          //返回成功
                    }
                    else if (res_code == 273) //用户不存在
                    {
                        msg.Message = "修改失败,UMC中不存在此用户.";
                        return(msg);
                    }
                    else
                    {
                        msg.Message = "修改失败,UMC错误码为:" + res_code.ToString();
                        return(msg);
                    }
                }
                else
                {
                    msg.Message = "返回的信息中未找到修改完成信息,返回内容为:" + msg1.Result;
                    return(msg);
                }
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
                return(msg);
            }
        }
        /// <summary>
        /// 发送数据,发送前后都做登录校验
        /// </summary>
        /// <param name="Uri">调用方法的URI</param>
        /// <param name="RequestBody">发送的参数</param>
        /// <param name="OperationResult">调用方法的返回关键字,用于比对结果</param>
        /// <param name="ResultMsg">返回0则成功,发送结果则resultMsg中,非0则为失败,错误信息则resultMsg中</param>
        /// <returns>返回0则成功,非0则为失败</returns>
        private static ReturnMessage SendJsonData(string Uri, StringContent RequestBody, string OperationResult)
        {
            ReturnMessage msg = new ReturnMessage
            {
                Succeed = false
            };

            try
            {
                if (!IsConnected) //如果没登录,则登录
                {
                    ReturnMessage msg1 = Login();
                    if (!msg1.Succeed)
                    {
                        msg.Message = "登录UMC失败," + msg1.Message;
                        return(msg);
                    }
                }

                //第一次发送数据
                HttpResponseMessage response = clientUMC.PostAsync(Uri, RequestBody).Result;

                //读取结果
                string result = response.Content.ReadAsStringAsync().Result;

                //处理结果
                if (result.IndexOf("/ipsimatic-logon\", \"nonce\":\"") != -1) //返回中含有【 /ipsimatic-logon","nonce":"】字符串说明登录超时,重新登录
                {
                    IsConnected = false;
                    ReturnMessage msg1 = Login();
                    if (!msg1.Succeed)
                    {
                        msg.Message = "登录UMC失败," + msg1.Message;
                        return(msg);
                    }

                    //登录后再次发送数据和读取结果
                    response = clientUMC.PostAsync(Uri, RequestBody).Result;
                    result   = response.Content.ReadAsStringAsync().Result;
                }

                if (result.IndexOf("<html>") != -1) //说明返回的不是json
                {
                    msg.Message = "调用登录Web API出错:" + result;
                    return(msg);
                }

                JObject parsedJson = JObject.Parse(result);
                JToken  res_Operation;
                if ((parsedJson.TryGetValue("operation", out res_Operation))) //找到操作标记
                {
                    if (res_Operation.ToString() == OperationResult)          //是当前操作返回
                    {
                        JToken res_code;
                        if ((parsedJson.TryGetValue("result", out res_code))) //找到结果标记
                        {
                            if (res_code.ToString() == "0")                   //0为成功
                            {
                                msg.Succeed = true;
                                msg.Result  = result; //返回成功的详细信息
                                return(msg);
                            }
                            else
                            {
                                msg.Message = "操作失败,返回状态为:" + res_code.ToString();
                                return(msg);
                            }
                        }
                        else
                        {
                            msg.Message = "未找当前操作返回结果,返回内容为:" + result;
                            return(msg);
                        }
                    }
                    else
                    {
                        msg.Message = "返回的不是当前操作结果,返回内容为:" + result;
                        return(msg);
                    }
                }
                else
                {
                    msg.Message = "未找到操作返回信息,返回内容为:" + result;
                    return(msg);
                }
            }
            catch (System.Exception ex)
            {
                msg.Message = ex.Message;
                return(msg);
            }
        }
        private static ReturnMessage Login() //登录
        {
            ReturnMessage msg = new ReturnMessage
            {
                Succeed = false
            };

            try
            {
                if ((BaseURL == null) || (BaseURL == ""))
                {
                    IsConnected = false;
                    msg.Message = "请先使用Initialize方法进行初始化.";
                    return(msg);
                }

                string uri = string.Format("{0}?user={1}&password={2}", LoginURI, AdminUserName, AdminPassword);
                HttpResponseMessage response = clientUMC.GetAsync(uri).Result;

                //var cookies = response.Headers.GetValues("Set-Cookie");

                //读取结果
                string result = response.Content.ReadAsStringAsync().Result;

                if (result.IndexOf("<html>") != -1) //说明返回的不是json
                {
                    IsConnected = false;
                    msg.Message = "调用登录Web API出错:" + result;
                    return(msg);
                }

                JObject parsedJson = JObject.Parse(result);
                JToken  res_Operation;
                if ((parsedJson.TryGetValue("operation", out res_Operation))) //找到登录操作标记
                {
                    if (res_Operation.ToString() == "loginresult")            //是登录返回
                    {
                        JToken res_code;
                        if ((parsedJson.TryGetValue("result", out res_code))) //找到结果标记
                        {
                            if (res_code.ToString() == "0")                   //0为成功
                            {
                                IsConnected = true;
                                msg.Succeed = true;
                                return(msg);
                            }
                            else
                            {
                                switch (res_code.ToString())
                                {
                                case "2":
                                    msg.Message = "登录失败,管理员用户 " + AdminUserName + " 已被锁定,请至UMC中解除锁定";
                                    break;

                                case "3":
                                    msg.Message = "登录失败,管理员用户 " + AdminUserName + " 已被停用,请至UMC中启用";
                                    break;

                                case "4":
                                    msg.Message = "登录失败,管理员用户名 " + AdminUserName + " 或密码错误";
                                    break;

                                case "6":
                                    msg.Message = "登录失败,必须至UMC更改管理员用户 " + AdminUserName + " 的密码";
                                    break;

                                case "7":
                                    msg.Message = "登录失败,管理员用户 " + AdminUserName + " 的密码过期,请至UMC中重设密码";
                                    break;

                                default:
                                    msg.Message = "登录失败,返回状态为:" + res_code.ToString();
                                    break;
                                }

                                IsConnected = false;
                                return(msg);
                            }
                        }
                        else
                        {
                            IsConnected = false;
                            msg.Message = "未找到登录返回结果,返回内容为:" + result;
                            return(msg);
                        }
                    }
                    else
                    {
                        IsConnected = false;
                        msg.Message = "返回的不是当前登录操作结果,返回内容为:" + result;
                        return(msg);
                    }
                }
                else
                {
                    IsConnected = false;
                    msg.Message = "未找到操作返回信息,返回内容为:" + result;
                    return(msg);
                }
            }
            catch (Exception ex)
            {
                IsConnected = false;
                msg.Message = GetAllErrorMsg(ex);
                return(msg);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="umc">传入UMC对象</param>
        /// <returns></returns>
        private ReturnMessage CreateUser(UmcUser umc)
        {
            ReturnMessage msg = new ReturnMessage
            {
                Succeed = false
            };

            try
            {
                if (umc == null || umc.Users == null || umc.Users.Count == 0)
                {
                    msg.Message = "无传入过来的用户名,请查看接口信息";
                    return(msg);
                }

                IList <string> errUserList = new List <string>();
                foreach (User entity in umc.Users)
                {
                    try
                    {
                        ReturnMessage gumsg = RoleManagement.GetUser(entity.Name);
                        if (!gumsg.Succeed)
                        {
                            errUserList.Add(string.Format("查询用户[{0}]失败,原因:[{1}]", entity.Name, gumsg.Message));
                            continue;
                        }

                        if (gumsg.Result != null)
                        {
                            JObject parsedJson1 = JObject.Parse(gumsg.Result.ToString());
                            JToken  res_Users;
                            if ((parsedJson1.TryGetValue("value", out res_Users))) //找到用户列表
                            {
                                List <User> roles  = (List <User>)JsonConvert.DeserializeObject(res_Users.ToString(), typeof(List <User>));
                                List <User> exists = roles.Where(p => p.Name == entity.Name).ToList <User>();
                                if (exists != null && exists.Count > 0)
                                {
                                    errUserList.Add(string.Format("用户[{0}]已存在,不允许创建重复用户", entity.Name));
                                    continue;
                                }
                            }
                        }

                        ReturnMessage ret = UserManagement.AddUser(entity.Name, _DefaultPassword, entity.FullName);
                        if (!ret.Succeed)
                        {
                            errUserList.Add(string.Format("创建用户[{0}]失败,原因:[{1}]", entity.Name, ret.Message));
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        errUserList.Add(string.Format("创建用户[{0}]失败,原因:[{1}]", entity.Name, ex.Message));
                    }
                    msg.Succeed = true;
                }
                msg.Result = errUserList;
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
            }
            return(msg);
        }
Esempio n. 7
0
        /// <summary>
        /// 关联角色与用户
        /// </summary>
        /// <param name="umc">传入UMC对象</param>
        /// <returns></returns>
        private ReturnMessage CreateUserRoleAssociation(UmcUser umc)
        {
            ReturnMessage msg = new ReturnMessage
            {
                Succeed = false
            };

            try
            {
                if (umc == null || umc.UserRoles == null || umc.UserRoles.Count == 0)
                {
                    msg.Message = "无用户与角色关联信息";
                    return(msg);
                }

                ReturnMessage rolemsg = RoleManagement.GetFullRoles();
                if (!rolemsg.Succeed)
                {
                    msg.Message = string.Format("角色获取失败,原因:[{0}]", rolemsg.Message);
                    return(msg);
                }

                IList <string> errUserRoleList = new List <string>();
                foreach (UserRole entity in umc.UserRoles)
                {
                    try
                    {
                        JObject parsedJson = JObject.Parse(rolemsg.Result.ToString());
                        JToken  res_Roles;
                        if ((parsedJson.TryGetValue("value", out res_Roles))) //找到用户列表
                        {
                            List <Role> roles  = (List <Role>)JsonConvert.DeserializeObject(res_Roles.ToString(), typeof(List <Role>));
                            List <Role> exists = roles.Where(p => p.Name == entity.RoleName).ToList <Role>();
                            if (exists == null || exists.Count == 0)
                            {
                                errUserRoleList.Add(string.Format("角色[{0}]不存在,不允许创建用户[{1}]关联", entity.RoleName, entity.UserName));
                                continue;
                            }
                        }

                        ReturnMessage gumsg = RoleManagement.GetUser(entity.UserName);
                        if (!gumsg.Succeed)
                        {
                            errUserRoleList.Add(string.Format("查询用户[{0}]失败,原因:[{1}]", entity.UserName, gumsg.Message));
                            continue;
                        }

                        JObject parsedJson1 = JObject.Parse(gumsg.Result.ToString());
                        JToken  res_Users;
                        User    relUser = null;
                        if ((parsedJson1.TryGetValue("value", out res_Users))) //找到用户列表
                        {
                            List <User> roles  = (List <User>)JsonConvert.DeserializeObject(res_Users.ToString(), typeof(List <User>));
                            List <User> exists = roles.Where(p => p.Name == entity.UserName).ToList <User>();
                            if (exists == null || exists.Count == 0)
                            {
                                errUserRoleList.Add(string.Format("用户[{0}]不存在,不允许创建用户[{1}]关联", entity.UserName, entity.RoleName));
                                continue;
                            }

                            relUser = exists[0];
                        }

                        if (relUser == null)
                        {
                            errUserRoleList.Add(string.Format("用户[{0}]不存在,不允许创建用户[{1}]关联", entity.RoleName, entity.UserName));
                            continue;
                        }

                        ReturnMessage relationmsg = RoleManagement.CreateUserRoleAssociation(entity.RoleName, new string[] { relUser.Id });
                        if (!relationmsg.Succeed)
                        {
                            errUserRoleList.Add(string.Format("创建用户[{0}]与角色关联[{1}]失败,原因:[{2}]", entity.UserName, entity.RoleName, relationmsg.Message));
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        errUserRoleList.Add(string.Format("创建用户[{0}]与角色关联[{1}]失败,原因:[{2}]", entity.UserName, entity.RoleName, ex.Message));
                    }
                    msg.Succeed = true;
                }
                msg.Result = errUserRoleList;
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
            }
            return(msg);
        }
Esempio n. 8
0
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="umc">传入UMC对象</param>
        /// <returns></returns>
        private ReturnMessage CreateRole(UmcUser umc)
        {
            ReturnMessage msg = new ReturnMessage
            {
                Succeed = false
            };

            try
            {
                if (umc == null || umc.Roles == null || umc.Roles.Count == 0)
                {
                    msg.Message = "无传入过来的角色,请查看接口信息";
                    return(msg);
                }

                ReturnMessage gumsg = RoleManagement.GetFullRoles();
                if (!gumsg.Succeed)
                {
                    msg.Message = string.Format("角色获取失败,原因:[{0}]", gumsg.Message);
                    return(msg);
                }

                IList <string> errRoleList = new List <string>();
                foreach (Role entity in umc.Roles)
                {
                    try
                    {
                        JObject parsedJson = JObject.Parse(gumsg.Result.ToString());
                        JToken  res_Roles;
                        if ((parsedJson.TryGetValue("value", out res_Roles))) //找到用户列表
                        {
                            List <Role> roles  = (List <Role>)JsonConvert.DeserializeObject(res_Roles.ToString(), typeof(List <Role>));
                            List <Role> exists = roles.Where(p => p.Name == entity.Name).ToList <Role>();
                            if (exists != null && exists.Count > 0)
                            {
                                errRoleList.Add(string.Format("角色[{0}]已经存在,不允许重复创建", entity.Name));
                                continue;
                            }
                        }

                        ReturnMessage ret = RoleManagement.CreateRole(entity.Name, entity.Description);
                        if (!ret.Succeed)
                        {
                            errRoleList.Add(string.Format("创建角色[{0}]失败,原因:[{1}]", entity.Name, ret.Message));
                            continue;
                        }

                        //下面这个动作有bug,创建完成角色之后,在Stuido里自行进行设置@hengde 2019-09-21
                        //ReturnMessage ret_attr = RoleManagement.AddFunctionRightsToRole(entity.Name);
                        //if (!ret_attr.Succeed)
                        //{
                        //    errRoleList.Add(string.Format("角色[{0}]权限赋值失败,原因:[{1}]", entity.Name, ret_attr.Message));
                        //    continue;
                        //}
                        msg.Succeed = true;
                    }
                    catch (Exception ex)
                    {
                        errRoleList.Add(string.Format("创建角色[{0}]失败,原因:[{1}]", entity.Name, ex.Message));
                    }
                }
                msg.Result = errRoleList;
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
            }
            return(msg);
        }