/// <summary> /// Adds the specified user. /// </summary> /// <param name="user">The user.</param> /// <param name="language">The language.</param> /// <param name="message">The message.</param> /// <returns></returns> public static bool Add(User user, string language, out string message) { if (user == null || string.IsNullOrEmpty(user.Name)) { message = MessageHelper.GetMessage(ErrorNumber.BadRequest, language); return false; } try { int totalCount; var db = DBHelper.Open(); List<dynamic> list = db.Users.Query() .Where(db.Users.Name == user.Name) .WithTotalCount(out totalCount) .ToList(); if (0 == totalCount) { db.Users.Insert(user); message = MessageHelper.GetMessage(ActNumber.Add, DataTypeNumber.User, Result.Success, language, user.Name); return true; } else { message = MessageHelper.GetMessage(DataTypeNumber.User, Exist.Exist, language, user.Name); return false; } } catch (Exception e) { message = e.ToString(); return false; } }
/// <summary> /// Logins the specified request. /// 服务端处理登录请求 /// </summary> /// <param name="request">The request.</param> /// <param name="result">The result.</param> /// <param name="language">The language.</param> public static void Login(JObject request, ref JObject result, string language, ref Session session) { var context = OperationContext.Current;//提供方法执行的上下文环境 var properties = context.IncomingMessageProperties;//获取传进的消息属性 var endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;//获取消息发送的远程终结点IP和端口 if(endpoint==null) { AddFailInfo(ref result, ErrorNumber.SeeDetail.ToString(), MessageHelper.GetMessage(ErrorNumber.FailToGetClientInfo, language)); return; } string message; UserType type; User user = new User { Name = request[LoginFieldKeyword.User].ToString(), Passwd = request[LoginFieldKeyword.Passwd].ToString() }; if (UserHelper.VerifyUser(user, out type, language, out message)) { if(!user.IsEnabled) { AddFailInfo(ref result, ErrorNumber.SeeDetail.ToString(), MessageHelper.GetMessage(ErrorNumber.UserIsDisabled, language, user.Name)); return; } var loginTime = DateTime.Now; var lastuser = UserHelper.Get(user.Name, out message, language); session = new Session { UserType = type, UserName = lastuser.Name, Language = language }; string token = _session.Join(request[LoginFieldKeyword.User].ToString(), session); var resultObject = new LoginResult { Token = token, UserType = type.ToString(), LastAddress = lastuser.LastLoginIP, LastDatetime = lastuser.LastLoginTime, Address = endpoint.Address, Datetime = loginTime, UserName = lastuser.Name }; if (!UserHelper.UpdateLoginInfo(user, loginTime, endpoint.Address, language, out message)) { AddFailInfo(ref result, ErrorNumber.SeeDetail.ToString(), message); } AddSuccessInfo(ref result, ResultType.List, resultObject, MessageHelper.GetMessage(ErrorNumber.LoginSuccess, language)); } else { AddFailInfo(ref result, ErrorNumber.SeeDetail.ToString(), message); } }
public static bool VerifyUser(User user, out UserType type, string language, out string message) { message = ""; if (user == null || string.IsNullOrEmpty(user.Name)) { message = MessageHelper.GetMessage(ErrorNumber.BadRequest, language); type = UserType.Null; return false; } try { int totalCount; var db = DBHelper.Open(); List<dynamic> list = db.Users.Query() .Where(db.Users.Name == user.Name) .WithTotalCount(out totalCount) .ToList(); if (1 == totalCount) { if (KORT.Util.Tools.CryptoString(user.Passwd) == list[0].Passwd) { message = MessageHelper.GetMessage(ActNumber.Get, DataTypeNumber.User, Result.Success, language, user.Name); type = (UserType)list[0].Type; user.IsEnabled = list[0].IsEnabled; return true; } } message = MessageHelper.GetMessage(ErrorNumber.WrongUserOrPasswd, language, ""); type = UserType.Null; return false; } catch (Exception e) { message = e.ToString(); type = UserType.Null; return false; } }
public static bool UpdateLoginInfo(User user, DateTime lastLoginTime, string lastLoginIP, string language, out string message) { if (user == null || string.IsNullOrEmpty(user.Name)) { message = MessageHelper.GetMessage(ErrorNumber.BadRequest, language); return false; } try { int totalCount; var db = DBHelper.Open(); List<dynamic> list = db.Users.Query() .Where(db.Users.Name == user.Name) .WithTotalCount(out totalCount) .ToList(); if (0 == totalCount) { message = MessageHelper.GetMessage(DataTypeNumber.User, Exist.NotExist, language, user.Name); return false; } user.LastLoginTime = list[0].LastLoginTime; user.LastLoginIP = list[0].LastLoginIP; list[0].LastLoginTime = lastLoginTime; list[0].LastLoginIP = lastLoginIP; db.Users.Update(list[0]); message = MessageHelper.GetMessage(ActNumber.Update, DataTypeNumber.User, Result.Success, language, user.Name); return true; } catch (Exception e) { message = e.ToString(); return false; } }
//modify public static bool Modify(UserType actor, string actorName, User user, string language, out string message) { if (user == null || string.IsNullOrEmpty(user.Name)) { message = MessageHelper.GetMessage(ErrorNumber.BadRequest, language); return false; } try { int totalCount; var db = DBHelper.Open(); List<dynamic> list = db.Users.Query() .Where(db.Users.Name == user.Name) .WithTotalCount(out totalCount) .ToList(); if (0 == totalCount) { message = MessageHelper.GetMessage(DataTypeNumber.User, Exist.NotExist, language, user.Name); return false; } if (actor == UserType.Super) { if (!CheckModifyType((UserType)list[0].Type, user.Type, language, out message) || !CheckModifyPasswd(list[0].Passwd, user.Passwd, language, out message) || !CheckModifyIsEnabled(list[0].IsEnabled, user.IsEnabled, language, out message)) return false; list[0].Type = user.Type; if (!string.IsNullOrEmpty(user.Passwd)) //Fix: if do not modify password, leave it empty. do not allow empty password. list[0].Passwd = user.Passwd; list[0].IsEnabled = user.IsEnabled; db.Users.Update(list[0]); message = MessageHelper.GetMessage(ActNumber.Modify, DataTypeNumber.User, Result.Success, language, user.Name); return true; } else if (actorName == list[0].Name) { if (!CheckModifyPasswd(list[0].Passwd, user.Passwd, language, out message)) return false; list[0].Passwd = user.Passwd; db.Users.Update(list[0]); message = MessageHelper.GetMessage(ActNumber.Modify, DataTypeNumber.User, Result.Success, language, user.Name); return true; } else { message = MessageHelper.GetMessage(ActNumber.Modify, DataTypeNumber.User, Result.Fail, language, user.Name); return false; } } catch (Exception e) { message = e.ToString(); return false; } }