public static void Operated(this Entities db, OperationType operationType, User user, Department campus) { try { if (user == null || campus == null) return; var ol = new OperationLog(); ol.Id = db.GetId(); ol.Time = DateTime.Now; ol.Type = operationType; ol.UserId = user.Id; ol.CampusId = campus.Id; db.OperationLog.Add(ol); db.SaveChanges(); } catch { } }
protected bool IsSsoOnline(out User user) { try { var onlineStringId = string.Empty; if (Session[HomoryConstant.SessionOnlineId] != null) onlineStringId = Session[HomoryConstant.SessionOnlineId].ToString(); else if (Request.Cookies.AllKeys.Contains(HomoryConstant.CookieOnlineId)) { var httpCookie = Request.Cookies[HomoryConstant.CookieOnlineId]; if (httpCookie != null) { var value = httpCookie.Value; HttpContext.Current.Session[HomoryConstant.SessionOnlineId] = Guid.Parse(value); onlineStringId = value; } } if (string.IsNullOrEmpty(onlineStringId)) { user = null; return false; } var onlineId = Guid.Parse(onlineStringId); var online = HomoryContext.Value.UserOnline.SingleOrDefault(o => o.Id == onlineId); if (online == null) { Session.Remove(HomoryConstant.SessionOnlineId); if (Request.Cookies.AllKeys.Contains(HomoryConstant.CookieOnlineId)) { var cookie = Request.Cookies[HomoryConstant.CookieOnlineId]; if (cookie != null) { cookie.Expires = DateTime.Now.AddSeconds(-1); Response.SetCookie(cookie); } } user = null; return false; } else { var cookie = new HttpCookie(HomoryConstant.CookieOnlineId, online.Id.ToString().ToUpper()); var expire = int.Parse(HomoryContext.Value.ApplicationPolicy.Single(o => o.Name == "UserCookieExpire" && o.ApplicationId == Guid.Empty).Value); cookie.Expires = DateTime.Now.AddMinutes(expire); HttpContext.Current.Response.SetCookie(cookie); online.TimeStamp = DateTime.Now; HomoryContext.Value.SaveChanges(); } user = online.User; return true; } catch { user = null; return false; } }
protected void RedierectPermanent(User user) { if (!string.IsNullOrEmpty(Request.QueryString["Permanent"]) && Request.QueryString["Permanent"].Equals("Jcms", StringComparison.OrdinalIgnoreCase)) { try { string url = string.Format("Jcms".FromWebConfig(), user.UserOnline.First().Id.ToString(), user.Teacher.AutoId.ToString(), Server.UrlEncode(user.Account.Trim()), Server.UrlEncode(user.RealName.Trim()), Server.UrlEncode(user.DepartmentUser.First(o => (o.Type == DepartmentUserType.部门主职教师 || o.Type == DepartmentUserType.借调后部门主职教师) && o.State < State.审核).TopDepartment.Name.Trim())); Response.Redirect(url, false); } catch { string url = string.Format("Jcms".FromWebConfig(), "", "", "", "", ""); Response.Redirect(url, false); } } else if (!string.IsNullOrEmpty(Request.QueryString["Permanent"]) && Request.QueryString["Permanent"].Equals("JMail", StringComparison.OrdinalIgnoreCase)) { try { string url = string.Format("JMail".FromWebConfig(), user.UserOnline.First().Id.ToString(), user.Teacher.AutoId.ToString(), Server.UrlEncode(user.Account.Trim()), Server.UrlEncode(user.RealName.Trim()), Server.UrlEncode(user.DepartmentUser.First(o => (o.Type == DepartmentUserType.部门主职教师 || o.Type == DepartmentUserType.借调后部门主职教师) && o.State < State.审核).TopDepartment.Name.Trim())); Response.Redirect(url, false); } catch { string url = string.Format("JMail".FromWebConfig(), "", "", "", "", ""); Response.Redirect(url, false); } } }
public dynamic UserRegister(Entities db, Guid id, string email, string password1, string password2, string name, string realName, string iconUrl, string description) { dynamic output = new ExpandoObject(); output.Ok = false; output.Data = new ExpandoObject(); try { var open = db.ApplicationPolicy.Where(o => o.Name == "UserRegistration" && o.ApplicationId == Guid.Empty) .FutureFirstOrDefault(); var mRegex = db.ApplicationPolicy.Where(o => o.Name == "UserEmailRegex" && o.ApplicationId == Guid.Empty).FutureFirstOrDefault(); var length = db.ApplicationPolicy.Where(o => o.Name == "UserPasswordLength" && o.ApplicationId == Guid.Empty) .FutureFirstOrDefault(); var count = db.User.Where(o => o.Account == email && o.State < State.审核).FutureCount(); if (!bool.Parse(open.Value.Value)) { output.Data.Message = "用户注册功能暂未开放"; output.Data.Parameter = string.Empty; return output; } if (string.IsNullOrWhiteSpace(email) || !new Regex(mRegex.Value.Value).IsMatch(email)) { output.Data.Message = "请输入.com/.cn结尾的电子邮箱"; output.Data.Parameter = "email"; return output; } if (string.IsNullOrWhiteSpace(password1) || string.IsNullOrWhiteSpace(password2) || !password1.Equals(password2, StringComparison.Ordinal) || password1.Length < int.Parse(length.Value.Value)) { output.Data.Message = string.Format("请输入不少于{0}位的密码,并确保两次输入的密码一致", length); output.Data.Parameter = "password"; return output; } string key, salt; var password = HomoryCryptor.Encrypt(password1, out key, out salt); if (count.Value == 0) { var user = new User { Id = id, Account = email, RealName = string.IsNullOrWhiteSpace(realName) ? (string.IsNullOrWhiteSpace(name) ? email : name) : realName, DisplayName = string.IsNullOrWhiteSpace(name) ? email : name, Icon = iconUrl, Stamp = Guid.NewGuid(), Password = password, PasswordEx = null, CryptoKey = key, CryptoSalt = salt, Type = UserType.注册, State = State.默认, Ordinal = 0, Description = string.IsNullOrWhiteSpace(description) ? null : description }; db.User.Add(user); db.SaveChanges(); output.Data.Entity = user; output.Ok = true; return output; } output.Data.Message = "电子邮件已被注册"; output.Data.Parameter = string.Empty; return output; } catch (Exception exception) { output.Data.Message = exception.Message; return output; } }
public bool StudentAdd(Entities db, Guid campusId, Guid classId, int ordinal, string name, string account, string passwordInitial, State state, string uniqueId, string idCard, bool? gender, DateTime? birthday, string nationality, string birthplace, string address, string charger, string chargerContact) { try { string key, salt; var password = HomoryCryptor.Encrypt(passwordInitial, out key, out salt); if (HomoryContext.Value.Student.Count(o => o.IDCard == idCard && o.UniqueId == uniqueId && o.User.State == State.启用) == 0) { var user = new User { Id = db.GetId(), Account = account, RealName = name, DisplayName = name, Stamp = Guid.NewGuid(), Type = UserType.学生, Password = password, PasswordEx = null, CryptoKey = key, CryptoSalt = salt, Icon = "~/Common/默认/用户.png", State = state, Ordinal = ordinal, Description = null }; var userStudent = new Homory.Model.Student { Id = user.Id, UniqueId = uniqueId, Gender = gender, Birthday = birthday, Birthplace = birthplace, Address = address, Nationality = nationality, IDCard = idCard, Charger = charger, ChargerContact = chargerContact }; var relation = new DepartmentUser { DepartmentId = classId, UserId = user.Id, TopDepartmentId = campusId, Type = DepartmentUserType.班级学生, State = State.启用, Ordinal = 0, Time = DateTime.Now }; db.User.Add(user); db.Student.Add(userStudent); db.DepartmentUser.Add(relation); } else { var nowU = HomoryContext.Value.Student.Single(o => o.IDCard == idCard && o.UniqueId == uniqueId && o.User.State == State.启用); var nowQ = HomoryContext.Value.DepartmentUser.Where(o => o.DepartmentId == classId && o.UserId == nowU.Id && o.Type == DepartmentUserType.班级学生); if (nowQ.Count() == 0) { var relation = new DepartmentUser { DepartmentId = classId, UserId = nowU.Id, TopDepartmentId = campusId, Type = DepartmentUserType.班级学生, State = State.启用, Ordinal = 0, Time = DateTime.Now }; db.DepartmentUser.Add(relation); } else { HomoryContext.Value.DepartmentUser.Where(o => o.DepartmentId == classId && o.UserId == nowU.Id && o.Type == DepartmentUserType.班级学生).Update(o => new DepartmentUser { Time = DateTime.Now, State = State.启用 }); } } db.SaveChanges(); return true; } catch { return false; } }