예제 #1
0
        public IActionResult Login([Required] String userId, [Required] String userPassword)
        {
            if (ModelState.IsValid)
            {
                userId       = userId.Trim();
                userPassword = userPassword.Trim();

                //判断用户身份
                UserType type = _analysis.GetUserType(userId);
                if (type == UserType.Anonymous) //匿名用户
                {
                    return(Json(new
                    {
                        isOk = false,
                        message = "账户不存在!如果你的账号尚未录入请联系系统维护人员录入!",
                    }));
                }
                //如果是管理员判断密码是否正确
                if (type == UserType.Principal)
                {
                    Principal principal = _context.Principals.Find(userId);
                    if (_ncryption.DecryptByRsa(principal.Password) != _ncryption.EncodeByMd5(_ncryption.EncodeByMd5(userPassword)))
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "管理员的密码不正确!",
                        }));
                    }
                }
                //如果是学生判断密码是正确
                if (type == UserType.Student)
                {
                    if (!_context.Student.Any(stu => stu.Password == _ncryption.EncodeByMd5(_ncryption.EncodeByMd5(userPassword))))
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "同学你的密码不正确!忘记了可以修改密码。",
                        }));
                    }
                }
                //判断是否让管理员登录 超级管理员不被禁止登录
                SystemSetting setting = _config.LoadSystemSetting();
                if (type == UserType.Principal)
                {
                    Principal principal = _context.Principals.Find(userId);

                    if (!setting.LoginSetting.PrincipalLogin && principal.PrincipalStatus != PrincipalStatus.Super)
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "系统维护中,管理员请等待系统维护之后进入!",
                        }));
                    }
                    //判断此管理员是否已经被禁止
                    if (principal.PrincipalStatus == PrincipalStatus.Ban)
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "管理员,你已经被禁止登录!",
                        }));
                    }

                    //验证成功保存信息让其登录
                    LoginUserModel user = new LoginUserModel()
                    {
                        UserId       = userId,
                        UserPassword = userPassword,
                        LoginTime    = DateTime.Now,
                        UserType     = type
                    };
                    var            userData = JsonConvert.SerializeObject(user, Formatting.None);
                    ClaimsIdentity identity = new ClaimsIdentity();
                    identity.AddClaim(new Claim(ClaimTypes.Name, principal.Name)); //用户名 姓名
                    identity.AddClaim(new Claim(ClaimTypes.Role, "Principal"));    //角色
                    identity.AddClaim(new Claim(ClaimTypes.UserData, userData));   //用户数据
                    ClaimsPrincipal claimPrincipal = new ClaimsPrincipal(identity);

                    AuthenticationProperties property = new AuthenticationProperties
                    {
                        ExpiresUtc   = DateTimeOffset.UtcNow.AddHours(16),//保存 16小时
                        IsPersistent = true
                    };
                    //持久化 Cookie 浏览器关闭了 只有在IsPersistent为True时,才会在写入Cookie指定Expires
                    HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimPrincipal, property);

                    return(Json(new
                    {
                        isOk = true,
                        url = "/Principal/Index",
                        message = "登录成功!"
                    }));
                }
                else
                {
                    Student student = _context.Student.Find(userId);
                    if (!setting.LoginSetting.StudentLogin)
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "系统尚未允许学生登录!请等待通知...",
                        }));
                    }

                    //模块判断
                    if (!_context.InstituteToModules.Any(im => im.InstituteId == student.InstituteId))
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "你所在学院并没有被规划在考试模块内,你无法参与实验室安全学习...",
                        }));
                    }
                    InstituteToModule itm = _context.InstituteToModules.FirstOrDefault(m => m.InstituteId == student.InstituteId);

                    /* 学习任务安排 -- 判断是否安排了学习任务 */
                    if (!_context.VLearningMaps.Any(l => l.StudentId == student.StudentId && l.ModuleId == itm.ModuleId))
                    {
                        /* CourceView 自动统计了 每个课程的 在用的视频资源数量 */
                        List <vCourceMap> courseMaps = _context.VCourceMaps
                                                       .Where(vc => vc.ModuleId == itm.ModuleId && vc.RCount != 0 && vc.CourceStatus == CourceStatus.Using)
                                                       .ToList(); //找出在用的所有课程

                        /* 如果有学习任务*/
                        if (courseMaps.Count > 0)
                        {
                            //安排学习课程
                            foreach (var item in courseMaps)
                            {
                                Learing learning = new Learing
                                {
                                    StudentId = student.StudentId,
                                    CourceId  = item.CourceId,
                                    IsFinish  = false,
                                    AddTime   = DateTime.Now
                                };
                                _context.Learings.Add(learning);
                                //记录学习进度

                                List <Resource> resources = _context.Resources
                                                            .Where(r => r.CourceId == item.CourceId)
                                                            .Where(r => r.ResourceStatus == ResourceStatus.Using)
                                                            .Where(r => r.ResourceType == ResourceType.Vedio)
                                                            .ToList();

                                foreach (var res in resources)
                                {
                                    Progress progress = new Progress
                                    {
                                        AddTime    = DateTime.Now,
                                        NeedTime   = res.LengthOfStudy,
                                        StudyTime  = 0,
                                        StudentId  = student.StudentId,
                                        ResourceId = res.ResourceId
                                    };
                                    _context.Progresses.Add(progress);
                                }
                            }
                        }
                        /*如果没有学习任务*/
                    }
                    _context.SaveChanges();

                    LoginUserModel user = new LoginUserModel()
                    {
                        UserId       = userId,
                        UserPassword = userPassword,
                        LoginTime    = DateTime.Now,
                        UserType     = type
                    };
                    var            userData = JsonConvert.SerializeObject(user, Formatting.None);
                    ClaimsIdentity identity = new ClaimsIdentity();
                    identity.AddClaim(new Claim(ClaimTypes.Name, student.Name)); //用户名 姓名
                    identity.AddClaim(new Claim(ClaimTypes.Role, "Student"));    //角色
                    identity.AddClaim(new Claim(ClaimTypes.UserData, userData)); //用户数据
                    ClaimsPrincipal claimPrincipal = new ClaimsPrincipal(identity);

                    AuthenticationProperties property = new AuthenticationProperties
                    {
                        ExpiresUtc   = DateTimeOffset.UtcNow.AddHours(28),//保存28小时
                        IsPersistent = true
                    };
                    //持久化 Cookie 浏览器关闭了 只有在IsPersistent为True时,才会在写入Cookie指定Expires
                    HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimPrincipal, property);

                    return(Json(new
                    {
                        isOk = true,
                        url = "/Student/Index",
                        message = "登录成功!"
                    }));
                }
            }
            else
            {
                return(Json(new
                {
                    isOk = false,
                    message = "传递了错误的参数!无法登录",
                    url = "/Error/ParameterError"
                }));
            }
        }
예제 #2
0
        public IActionResult Pass([Required] int apId)
        {
            if (ModelState.IsValid)
            {
                if (!_analysis.GetLoginUserConfig(HttpContext).Power.StudentManager)
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误提示",
                        message = "你并无学生管理操作权限"
                    }));
                }
                LogPricipalOperation operation =
                    _logger.GetDefaultLogPricipalOperation(PrincpalOperationCode.InspectJoinApplication, $"{apId}", $"审核学生加入考试申请 通过审核");

                ApplicationJoinTheExamination applicationJoin = _context.ApplicationJoinTheExaminations.Find(apId);

                if (applicationJoin != null)
                {
                    //是否已经存在了
                    if (_context.Student.Any(s => s.StudentId == applicationJoin.StudentId))
                    {
                        _email.SendJoinEmail(applicationJoin.Email, applicationJoin.StudentId, applicationJoin.Name, applicationJoin.AddTime, false, "你已经在考试范围内!");
                        _context.SaveChanges();
                        return(Json(new
                        {
                            isOk = false,
                            title = "错误提示",
                            message = "审核结果: 此学生已经在考试范围内! 审核此申请失败."
                        }));
                    }
                    else
                    {
                        Student student = (Student)applicationJoin;
                        // //身份证后六位就是密码
                        student.Password = _encryption.EncodeByMd5(_encryption.EncodeByMd5(student.IDNumber.Substring(student.IDNumber.Length - 6, 6)));

                        SystemSetting setting = _config.LoadSystemSetting();
                        //如果这个学院有对应的模块 然后找到这个模块的 考试设置类
                        var insModule = _context.InstituteToModules.FirstOrDefault(im => im.InstituteId == student.InstituteId);
                        if (insModule != null)
                        {
                            //如果这个模块具有加载类
                            Boolean isHave = setting.ExamModuleSettings.TryGetValue(insModule.ModuleId, out var meSetting);
                            student.MaxExamCount = isHave? meSetting.AllowExamTime:2;
                        }
                        else
                        {
                            //如果学院灭有属于哪个模块
                            student.MaxExamCount = 2;
                        }

                        operation.PrincpalOperationStatus = PrincpalOperationStatus.Success; //日志记录 成功
                        applicationJoin.ApplicationStatus = ApplicationStatus.Pass;

                        _context.LogPricipalOperations.Add(operation);
                        _context.Student.Add(student);
                        _context.SaveChanges();

                        _email.SendJoinEmail(applicationJoin.Email, applicationJoin.StudentId, applicationJoin.Name, applicationJoin.AddTime, true, "");
                        return(Json(new
                        {
                            isOk = true,
                            title = "信息提示",
                            message = "审核完成!"
                        }));
                    }
                }
                else
                {
                    operation.PrincpalOperationStatus = PrincpalOperationStatus.Fail;
                    _logger.Logger(operation);
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误提示",
                        message = "申请不存在,或者已经被删除"
                    }));
                }
            }
            else
            {
                return(Json(new
                {
                    isOk = false,
                    error = _analysis.ModelStateDictionaryError(ModelState),
                    title = "错误提示",
                    message = "参数错误,传递了不符合规定的参数"
                }));
            }
        }
예제 #3
0
        /// <summary>
        /// 完成日志记录
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public IActionResult Create([Bind(include: "ModuleId,Content,Answer,Count,A,B,C,D,E,F")] SingleChoices item)
        {
            if (ModelState.IsValid)
            {
                if (!_analysis.GetLoginUserConfig(HttpContext).Power.QuestionBankManager)
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误提示",
                        message = "你并无题库管理操作权限"
                    }));
                }
                #region 功能实现区域
                LoginUserModel user = _analysis.GetLoginUserModel(HttpContext);
                String         Key  = _encryption.EncodeByMd5(item.Content.Trim());
                if (_context.SingleChoices.Any(s => s.Key == Key && s.ModuleId == item.ModuleId))
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误提示",
                        message = "你的题目已经存在! 重复题目无法加入"
                    }));
                }

                item.Content = item.Content.Trim();
                item.AddTime = DateTime.Now;
                item.Key     = Key;
                Char[] answer = item.Answer.ToUpper().Trim().ToCharArray();
                Array.Sort(answer);
                item.Answer             = String.Join("", answer); //答案全部大写
                item.A                  = item.A.Trim();
                item.B                  = item.B.Trim();
                item.C                  = item.C?.Trim();
                item.D                  = item.D?.Trim();
                item.E                  = item.E?.Trim();
                item.F                  = item.F?.Trim();
                item.Count              = item.Count;
                item.PrincipalId        = user.UserId;
                item.DegreeOfDifficulty = 1;
                LogPricipalOperation log = _logger.GetDefaultLogPricipalOperation(PrincpalOperationCode.SingleAdd,
                                                                                  $"查询编码:{item.SingleId}", $"添加单择题:{item.Content}!");

                log.PrincpalOperationStatus = PrincpalOperationStatus.Success;
                _context.LogPricipalOperations.Add(log);
                _context.SingleChoices.Add(item);
                _context.SaveChanges();
                return(Json(new
                {
                    isOk = true,
                    title = "消息提示",
                    message = "添加成功!"
                }));

                #endregion
            }
            else
            {
                List <string> sb   = new List <string>();
                List <string> Keys = ModelState.Keys.ToList();
                foreach (var key in Keys)
                {
                    var errors = ModelState[key].Errors.ToList();
                    //将错误描述添加到sb中
                    foreach (var error in errors)
                    {
                        sb.Add(error.ErrorMessage);
                    }
                }
                return(Json(new
                {
                    isOk = false,
                    error = sb,
                    title = "错误提示",
                    message = "参数错误,传递了不符合规定的参数"
                }));
            }
        }
예제 #4
0
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="judge"></param>
        /// <returns></returns>
        public IActionResult Create([Bind(include: "ModuleId,Content,Answer")] JudgeChoices judge)
        {
            if (ModelState.IsValid)
            {
                if (!_analysis.GetLoginUserConfig(HttpContext).Power.QuestionBankManager)
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误提示",
                        message = "你并无题库管理操作权限"
                    }));
                }

                LogPricipalOperation log = _logger.GetDefaultLogPricipalOperation(PrincpalOperationCode.AddJudge,
                                                                                  "查询编码:无", $"增加判断题 题目内容{judge.Content}");

                #region 功能实现区域
                LoginUserModel user = _analysis.GetLoginUserModel(HttpContext);
                String         Key  = _encryption.EncodeByMd5(judge.Content.Trim());
                if (_context.JudgeChoices.Any(j => j.Key == Key))
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误提示",
                        message = "你的题目已经存在! 重复题目无法加入"
                    }));
                }

                judge.Content            = judge.Content.Trim();
                judge.AddTime            = DateTime.Now;
                judge.Key                = Key;
                judge.Answer             = judge.Answer.ToUpper().Trim(); //答案全部大写
                judge.A                  = "是";
                judge.B                  = "否";
                judge.Count              = 2;
                judge.PrincipalId        = user.UserId;
                judge.DegreeOfDifficulty = 1;

                _context.JudgeChoices.Add(judge);
                _context.SaveChanges();

                log.PrincpalOperationName = $"查询编码:{judge.JudgeId}";
                _logger.Logger(log);
                return(Json(new
                {
                    isOk = true,
                    title = "消息提示",
                    message = "添加成功!"
                }));

                #endregion
            }
            else
            {
                List <string> sb   = new List <string>();
                List <string> Keys = ModelState.Keys.ToList();
                foreach (var key in Keys)
                {
                    var errors = ModelState[key].Errors.ToList();
                    //将错误描述添加到sb中
                    foreach (var error in errors)
                    {
                        sb.Add(error.ErrorMessage);
                    }
                }
                return(Json(new
                {
                    isOk = false,
                    error = sb,
                    title = "错误提示",
                    message = "参数错误,传递了不符合规定的参数"
                }));
            }
        }
예제 #5
0
        public IActionResult Create([Required] String id, [Required] String jobId, [Required] String name,
                                    [Required] String phone, [Required] String pwd)
        {
            if (ModelState.IsValid)
            {
                PrincipalConfig principalConfig = _analysis.GetLoginUserConfig(HttpContext);
                if (!principalConfig.Power.SystemManager)
                {
                    if (!_context.Principals.Any(p => p.PrincipalId == principalConfig.PrincipalId && p.PrincipalStatus == PrincipalStatus.Super))
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "你并无系统管理操作权限"
                        }));
                    }
                }
                if (_context.Student.Any(stu => stu.StudentId == id))
                {
                    return(Json(new
                    {
                        isOk = false,
                        message = $"编号:{id}是已经存在的学号!无法使用!"
                    }));
                }

                if (id.Length == 12 || id.Length == 10)
                {
                    return(Json(new
                    {
                        isOk = false,
                        message = $"编号:{id}属于学号范围!长度不能为10位或者12位 无法使用!"
                    }));
                }

                if (_context.Principals.Any(admin => admin.PrincipalId == id || admin.JobNumber == jobId))
                {
                    return(Json(new
                    {
                        isOk = false,
                        message = $"编号:{id}或者工号{jobId} 已经使用!"
                    }));
                }
                else
                {
                    Principal principal = new Principal
                    {
                        PrincipalId     = id,
                        JobNumber       = jobId,
                        Name            = name,
                        Phone           = phone,
                        PrincipalStatus = PrincipalStatus.Normal,
                        PrincipalConfig = $"{id}.json"
                    };
                    String password = _encryption.EncodeByRsa(_encryption.EncodeByMd5(_encryption.EncodeByMd5(pwd)));
                    principal.Password = password;
                    _context.Principals.Add(principal);

                    int result = _context.SaveChanges();

                    if (result == 1)
                    {
                        //配置权限
                        PrincipalConfig config = new PrincipalConfig
                        {
                            SettingTime = DateTime.Now, PrincipalId = id, Power = new Power()
                        };

                        using (var stream = new FileStream(
                                   Path.GetFullPath($@"{_hosting.ContentRootPath}/JsonConfig/{id}.json"), FileMode.Create,
                                   FileAccess.Write, FileShare.None))
                        {
                            using (var writer = new StreamWriter(stream))
                            {
                                JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings());
                                String         jsonResult = JsonConvert.SerializeObject(config, Formatting.Indented);
                                writer.Write(jsonResult);
                            }
                        }

                        return(Json(new
                        {
                            isOk = true,
                            message = "信息插入成功"
                        }));
                    }
                    else
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "信息插入失败"
                        }));
                    }
                }
            }
            else
            {
                return(Json(new
                {
                    isOk = false,
                    message = $"参数错误!输入了不合规范的参数。 "
                }));
            }
        }
예제 #6
0
        public IActionResult Create([Bind(include: "StudentId,IDNumber,InstituteId,Name,ProfessionId,BirthDate,Sex,StudentType,Grade,Email")] Student student)
        {
            if (ModelState.IsValid)
            {
                if (!_analysis.GetLoginUserConfig(HttpContext).Power.StudentManager)
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误",
                        message = "你并无学生管理操作权限"
                    }));
                }
                Institute  ins = _context.Institute.FirstOrDefault(i => i.InstituteId == student.InstituteId);
                Profession pro = _context.Professions.FirstOrDefault(p => p.ProfessionId == student.ProfessionId);
                if (ins == null)
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误",
                        message = "参数错误! 学院不存在! "
                    }));
                }
                if (pro == null)
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误",
                        message = "参数错误! 专业不存在! "
                    }));
                }
                if (pro.InstituteId != ins.InstituteId)
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误",
                        message = "此专业不属于此学院 !"
                    }));
                }
                if (_context.Student.Any(val => val.StudentId == student.StudentId))
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误",
                        message = "此学号的学生已经存在!"
                    }));
                }
                else
                {
                    var belong =
                        _context.InstituteToModules.FirstOrDefault(im => im.InstituteId == student.InstituteId);

                    if (belong != null)
                    {
                        SystemSetting setting  = _config.LoadSystemSetting();
                        Boolean       isConfig = setting.ExamModuleSettings.TryGetValue(belong.ModuleId, out var moduleExamSetting);
                        student.MaxExamCount = isConfig ? moduleExamSetting.AllowExamTime : 3;
                    }
                    else
                    {
                        student.MaxExamCount = 3; //系统默认考试次数三次
                    }

                    /* logger start */
                    LogPricipalOperation operation = _logger.GetDefaultLogPricipalOperation(
                        PrincpalOperationCode.AddStudent, $"{student.StudentId}",
                        $"增加学生 学号{student.InstituteId} 名称:{student.Name} ");
                    operation.PrincpalOperationStatus = PrincpalOperationStatus.Success;
                    /* logger end*/

                    student.IsPassExam   = false;
                    student.MaxExamScore = 0;
                    student.Password     = _ncryption.EncodeByMd5(_ncryption.EncodeByMd5(student.IDNumber.Substring(student.IDNumber.Length - 6, 6)));
                    _context.LogPricipalOperations.Add(operation);
                    _context.Student.Add(student);
                    _context.SaveChanges();
                    return(Json(new
                    {
                        isOk = true,
                        title = "温馨提示",
                        message = "添加成功!"
                    }));
                }
            }
            else
            {
                List <string> errorParamters = new List <string>();
                List <string> Keys           = ModelState.Keys.ToList();
                foreach (var key in Keys)
                {
                    var errors = ModelState[key].Errors.ToList();
                    foreach (var error in errors)
                    {
                        errorParamters.Add(error.ErrorMessage);
                    }
                }
                return(Json(new
                {
                    error = errorParamters,
                    isOk = false,
                    title = "错误",
                    message = "参数错误!传入了错误的信息! "
                }));
            }
        }