Ejemplo n.º 1
0
 public bool CourseAdd(Entities db, string name, State state, int ordinal, bool range)
 {
     try
     {
         var course = new Catalog
         {
             Id = db.GetId(),
             ParentId = HomoryCoreConstant.CourseOtherId,
             TopId = range ? Guid.Empty : (Guid?)null,
             Name = name,
             State = state,
             Ordinal = ordinal,
             Type = CatalogType.课程
         };
         var ex = db.Catalog.SingleOrDefault(o => o.Name == name && o.Type == CatalogType.课程);
         if (ex == null)
         {
             db.Catalog.Add(course);
             db.SaveChanges();
         }
         else
         {
             ex.State = state;
             ex.Ordinal = ordinal;
             db.SaveChanges();
         }
         return true;
     }
     catch
     {
         return false;
     }
 }
Ejemplo n.º 2
0
 public bool CourseTaught(Entities db, Guid classId, Guid courseId, Guid userId, State state)
 {
     try
     {
         CourseUntaught(db, classId, courseId);
         var taught = new Taught
         {
             DepartmentId = classId,
             CourseId = courseId,
             UserId = userId,
             Time = DateTime.Now,
             State = state
         };
         db.Taught.AddOrUpdate(taught);
         LogOp(OperationType.新增);
         db.SaveChanges();
         view.Rebind();
         viewX.Rebind();
         return true;
     }
     catch
     {
         return false;
     }
 }
Ejemplo n.º 3
0
 public bool CourseLearned(Entities db, Guid classId, Guid courseId)
 {
     try
     {
         if (db.Learned.Count(o => o.DepartmentId == classId && o.CourseId == courseId) > 0)
         {
             var learned = db.Learned.First(o => o.DepartmentId == classId && o.CourseId == courseId);
             learned.State = learned.State == State.启用 ? State.删除 : State.启用;
             LogOp(learned.State);
         }
         else
         {
             var learned = new Learned
             {
                 DepartmentId = classId,
                 CourseId = courseId,
                 State = State.启用
             };
             db.Learned.Add(learned);
             LogOp(OperationType.新增);
         }
         db.SaveChanges();
         return true;
     }
     catch
     {
         return false;
     }
 }
Ejemplo n.º 4
0
 public bool CourseUpdate(Entities db, Guid id, State state, int ordinal)
 {
     try
     {
         var course = db.Catalog.Single(o => o.Id == id);
         course.State = state;
         course.Ordinal = ordinal;
         db.SaveChanges();
         return true;
     }
     catch
     {
         return false;
     }
 }
Ejemplo n.º 5
0
 public bool UserSignOff(Entities db)
 {
     try
     {
         var oid = string.Empty;
         if (Session[HomoryConstant.SessionOnlineId] != null)
         {
             oid = Session[HomoryConstant.SessionOnlineId].ToString();
         }
         else if (Request.Cookies.AllKeys.Contains(HomoryConstant.CookieOnlineId))
         {
             var httpCookie = Request.Cookies[HomoryConstant.CookieOnlineId];
             if (httpCookie != null) oid = httpCookie.Value;
         }
         if (string.IsNullOrEmpty(oid))
         {
             return true;
         }
         var onlineGuid = Guid.Parse(oid);
         db.UserOnline.Where(o => o.Id == onlineGuid).Delete();
         db.SaveChanges();
         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);
                 HttpContext.Current.Response.SetCookie(cookie);
             }
         }
         return true;
     }
     catch
     {
         return false;
     }
 }
Ejemplo n.º 6
0
 public dynamic UserToVerify(Entities db, Guid id)
 {
     dynamic output = new ExpandoObject();
     output.Ok = false;
     output.Data = new ExpandoObject();
     output.Data.Message = "验证邮件发送失败";
     try
     {
         var user = db.User.Single(o => o.Id == id);
         user.State = State.审核;
         db.SaveChanges();
         var sender =
             db.ApplicationPolicy.Single(o => o.Name == "SmtpSender" && o.ApplicationId == Guid.Empty).Value;
         var content = string.Format(ToVerifyBody, user.DisplayName,
             Application["Sso"] + "Go/Verifying", user.Stamp, sender);
         output.Ok = Smtp.SendEmail(db, string.Format(ToVerifyHead, sender), user.DisplayName, content, user.Account, sender);
         return output;
     }
     catch
     {
         return output;
     }
 }
Ejemplo n.º 7
0
 protected dynamic UserSignOn(Entities db, string account, string password)
 {
     dynamic output = new ExpandoObject();
     output.Ok = false;
     output.Data = new ExpandoObject();
     try
     {
         var user = db.User.SingleOrDefault(o => o.Account == account && o.State < State.删除);
         if (user == null ||
             !password.Equals(HomoryCryptor.Decrypt(user.Password, user.CryptoKey, user.CryptoSalt),
                 StringComparison.OrdinalIgnoreCase))
         {
             output.Data.Message = "请输入正确的账号和密码";
             output.Entity = null;
             return output;
         }
         if (user.State == State.停用)
         {
             output.Data.Message = "用户已被停用";
             output.Entity = null;
             return output;
         }
         if (user.State == State.默认 || user.State == State.审核)
         {
             Session[HomoryConstant.SessionRegisterId] = user.Id;
             output.Ok = true;
             output.Data.Redirect = true;
             output.Data.RedirectUrl = Application["Sso"] + "Go/ToVerify";
             output.Entity = null;
             return output;
         }
         var online = user.UserOnline.SingleOrDefault();
         if (online == null)
         {
             online = new UserOnline
             {
                 Id = db.GetId(),
                 UserId = user.Id,
                 TimeStamp = DateTime.Now
             };
             db.UserOnline.Add(online);
         }
         else
         {
             online.TimeStamp = DateTime.Now;
         }
         db.SaveChanges();
         var cookie = new HttpCookie(HomoryConstant.CookieOnlineId, online.Id.ToString().ToUpper());
         var expire = int.Parse(db.ApplicationPolicy.Single(o => o.Name == "UserCookieExpire" && o.ApplicationId == Guid.Empty).Value);
         cookie.Expires = DateTime.Now.AddMinutes(expire);
         Response.SetCookie(cookie);
         Session[HomoryConstant.SessionOnlineId] = online.Id;
         output.Ok = true;
         output.Data.Redirect = true;
         var query = Server.UrlDecode(Request.QueryString["SsoRedirect"]);
         var query_x = Request.QueryString;
         if (string.IsNullOrEmpty(Request.QueryString["Permanent"]))
         {
             if (string.IsNullOrWhiteSpace(Request.QueryString["SsoRedirect"]))
             {
                 if (user.Type == UserType.教师)
                     output.Data.RedirectUrl = string.Format(string.Format("SsoRedirect".FromWebConfig() == "" ? Application["Sso"] + "Go/Board" : "SsoRedirect".FromWebConfig(), user.Account, HomoryCryptor.Decrypt(user.Password, user.CryptoKey, user.CryptoSalt)));
                 else
                     output.Data.RedirectUrl = string.Format(string.Format("SsoRedirectOther".FromWebConfig() == "" ? Application["Sso"] + "Go/Board" : "SsoRedirectOther".FromWebConfig(), user.Account, HomoryCryptor.Decrypt(user.Password, user.CryptoKey, user.CryptoSalt)));
             }
             else
             {
                 string url;
                 if (!string.IsNullOrWhiteSpace(Request.QueryString["OnlineId"]))
                 {
                     url = query;
                 }
                 else
                 {
                     if (query.IndexOf('&') <= 0)
                     {
                         url = string.Format("{0}?OnlineId={1}", query, user.UserOnline.Single().Id);
                     }
                     else
                     {
                         var index = query.IndexOf('&');
                         url =
                             Server.UrlDecode(query.Remove(index, 1)
                                 .Insert(index, string.Format("?OnlineId={0}&", user.UserOnline.Single().Id)));
                     }
                 }
                 foreach (var qx in query_x)
                 {
                     if (!qx.ToString().Equals("SsoRedirect", StringComparison.OrdinalIgnoreCase))
                         url += "&" + qx + "=" + query_x[qx.ToString()];
                 }
                 output.Entity = null;
                 if (string.IsNullOrWhiteSpace(url))
                 {
                     if (user.Type == UserType.教师)
                         string.Format(string.Format("SsoRedirect".FromWebConfig() == "" ? Application["Sso"] + "Go/Board" : "SsoRedirect".FromWebConfig(), user.Account, HomoryCryptor.Decrypt(user.Password, user.CryptoKey, user.CryptoSalt)));
                     else
                         string.Format(string.Format("SsoRedirectOther".FromWebConfig() == "" ? Application["Sso"] + "Go/Board" : "SsoRedirectOther".FromWebConfig(), user.Account, HomoryCryptor.Decrypt(user.Password, user.CryptoKey, user.CryptoSalt)));
                 }
                 else
                 {
                     output.Data.RedirectUrl = url;
                 }
             }
         }
         else
         {
             output.Entity = user;
             output.Data.RedirectUrl = "*Permanent*";
         }
         return output;
     }
     catch (Exception exception)
     {
         output.Data.Message = exception.Message;
         output.Entity = null;
         return output;
     }
 }
Ejemplo n.º 8
0
 public bool CourseUntaught(Entities db, Guid classId, Guid courseId)
 {
     try
     {
         db.Taught.Where(o => o.DepartmentId == classId && o.CourseId == courseId && o.State == State.启用).Update(o => new Taught { State = State.历史 });
         LogOp(OperationType.删除);
         db.SaveChanges();
         view.Rebind();
         viewX.Rebind();
         return true;
     }
     catch
     {
         return false;
     }
 }
Ejemplo n.º 9
0
        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;
            }
        }
Ejemplo n.º 10
0
 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;
     }
 }
Ejemplo n.º 11
0
 public bool StudentUpdate(Entities db, Guid id, int ordinal, string name, string account, State state, string uniqueId, string idCard, bool? gender, DateTime? birthday, string nationality, string birthplace, string address, string charger, string chargerContact)
 {
     try
     {
         if (db.User.Count(o => o.Id != id && o.Account == account) > 0)
             return false;
         var user = db.User.Single(o => o.Id == id);
         user.Account = account;
         user.RealName = name;
         user.DisplayName = name;
         user.Stamp = Guid.NewGuid();
         user.State = state;
         user.Ordinal = ordinal;
         var userStudent = db.Student.Single(o => o.Id == id);
         userStudent.UniqueId = uniqueId;
         userStudent.Gender = gender;
         userStudent.Birthday = birthday;
         userStudent.Birthplace = birthplace;
         userStudent.Address = address;
         userStudent.Nationality = nationality;
         userStudent.IDCard = idCard;
         userStudent.Charger = charger;
         userStudent.ChargerContact = chargerContact;
         var r = db.DepartmentUser.FirstOrDefault(o => o.UserId == id && o.Type == DepartmentUserType.班级学生);
         if (r != null)
         {
             r.Ordinal = ordinal;
             r.State = state == State.启用 ? State.启用 : State.历史;
         }
         db.SaveChanges();
         return true;
     }
     catch
     {
         return false;
     }
 }
Ejemplo n.º 12
0
        public bool StudentMove(Entities db, Guid id, Guid sourceDepartmentId, Guid targetDepartmentId, Guid targetCampusId)
        {
            try
            {
                var relation = db.DepartmentUser.Where(o => o.UserId == id && o.DepartmentId == sourceDepartmentId && o.Type == DepartmentUserType.班级学生 && o.State == State.启用).Update(o =>
                new DepartmentUser { State = State.历史 });
                var newRelation = new DepartmentUser
                {
                    DepartmentId = targetDepartmentId,
                    UserId = id,
                    TopDepartmentId = targetCampusId,
                    Type = DepartmentUserType.班级学生,
                    State = State.启用,
                    Time = DateTime.Now,
                    Ordinal = 0
                };
                db.DepartmentUser.AddOrUpdate(newRelation);
                db.SaveChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }