public override string[] GetRolesForUser(string login)
        {
            string[] role = new string[] { };
            using (HomeServerContext _db = new HomeServerContext())
            {
                try
                {
                    // Получаем пользователя
                    User user = (from u in _db.Users
                                 where u.Login == login
                                 select u).FirstOrDefault();
                    if (user != null)
                    {
                        // получаем роль
                        Role userRole = _db.Roles.Find(user.RoleId);

                        if (userRole != null)
                        {
                            role = new string[] { userRole.Name };
                        }
                    }
                }
                catch
                {
                    role = new string[] { };
                }
            }
            return role;
        }
        public override bool IsUserInRole(string username, string roleName)
        {
            bool outputResult = false;
            // Находим пользователя
            using (HomeServerContext _db = new HomeServerContext())
            {
                try
                {
                    // Получаем пользователя
                    User user = (from u in _db.Users
                                 where u.Login == username
                                 select u).FirstOrDefault();
                    if (user != null)
                    {
                        // получаем роль
                        Role userRole = _db.Roles.Find(user.RoleId);

                        //сравниваем
                        if (userRole != null && userRole.Name == roleName)
                        {
                            outputResult = true;
                        }
                    }
                }
                catch
                {
                    outputResult = false;
                }
            }
            return outputResult;
        }
Exemplo n.º 3
0
        public void AddUserToGroup(HomeServerContext context, int studentId, int groupId, DateTime date)
        {
            Student student = context.Students.Find(studentId);
            Group group = context.Groups.Find(groupId);

            if (student == null)
                return;

            if (group == null)
                return;

            if(group.Students.Contains(student))
                return;

            student.GroupId = group.Id;

            //get active packs for this group
            var temp = context.Packs.Where(p => p.Course.CourseStateId == 2).ToList();

            List<Pack> packs = new List<Pack>();

            foreach(var t in temp)
            {
                foreach(var g in t.Groups)
                {
                    if (g.Id == groupId)
                    {
                        packs.Add(t);
                    }
                }
            }

            //add visits and studentProcs for each packs
            foreach(var pack in packs)
            {
                foreach(var lesson in pack.Lessons)
                {
                    if(lesson.Date < date)
                        continue;

                    lesson.Visits.Add(new Visit()
                    {
                        StudentId = student.Id,
                        Values = new List<VisitValue>()
                        {
                            new VisitValue()
                            {
                                Value = VisitValue.DEFAULT_VALUE,
                                Version = 1
                            }
                        }
                    });
                }

                foreach(var workProc in pack.WorkProcs)
                {
                    var work = context.Works.Find(workProc.WorkId);
                    work.WorkStages = context.WorkStages.Where(ws => ws.WorkId == workProc.WorkId).ToList();

                    StudentWorkProc studentProc = new StudentWorkProc()
                    {
                        StudentId = student.Id,
                        Values = new List<StudentWorkProcValue>() { new StudentWorkProcValue() { Version = 1 } },
                        StudentStageProcs = new List<StudentStageProc>()
                    };

                    foreach (var workStage in work.WorkStages)
                    {
                        studentProc.StudentStageProcs.Add(new StudentStageProc()
                        {
                            WorkStageId = workStage.Id,
                            Values = new List<StudentStageProcValue>() { new StudentStageProcValue() { Version = 1 } }
                        });
                    }

                    workProc.StudentWorkProcs.Add(studentProc);
                }
            }

            StudentTransaction studentTransaction = new StudentTransaction()
            {
                Student = student,
                Group = group,
                Date = date
            };

            context.StudentTransaction.Add(studentTransaction);

            context.SaveChanges();
        }
Exemplo n.º 4
0
        public void RemoveUserFromGroup(HomeServerContext context, int studentId)
        {
            Student student = context.Students.Find(studentId);

            if (student == null)
            {
                return;
            }
            else
            {
                student.GroupId = null;

                StudentTransaction studentTransaction = new StudentTransaction()
                {
                    Student = student,
                    GroupId = null,
                    Date = DateTime.Now
                };

                context.StudentTransaction.Add(studentTransaction);

                context.SaveChanges();
            }
        }
        private bool ValidateUser(string login, string password)
        {
            bool isValid = false;

            using (HomeServerContext _db = new HomeServerContext())
            {
                try
                {
                    User user = (from u in _db.Users
                                 where u.Login == login && u.Password == password
                                 select u).FirstOrDefault();

                    if (user != null)
                    {
                        isValid = true;
                    }
                }
                catch(Exception e)
                {
                    isValid = false;
                }
            }
            return isValid;
        }
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody]NewCourseModel courseModel)
        {
            var course = courseModel.Course;
            var user = GetCurrentUser();

            //check existed course with same Year and Term
            var existedCourse = db.Courses.FirstOrDefault(c => c.TermId == course.Term.Id && c.Year == course.Year && c.UserId == user.Id);

            if(existedCourse != null)
            {
                HttpError err = new HttpError("У вас уже есть курс данного семестра на выбранный год");
                return Request.CreateResponse(HttpStatusCode.Forbidden, err);
            }

            var term = db.Terms.Find(course.Term.Id);

            var newCourse = new Course()
            {
                Term = term,
                Year = course.Year,
                User = user,
                CourseState = db.CourseStates.Find(1),
                Packs = new List<Pack>()
            };

            foreach (var pack in course.Packs)
            {
                var packGroupIds = pack.Groups.Select(g => g.Id);

                var packType = db.PackTypes.Find(pack.PackType.Id);
                var groups = db.Groups.Where(g => packGroupIds.Contains(g.Id)).ToList();

                newCourse.Packs.Add(new Pack()
                {
                    PackType = packType,
                    Groups = groups
                });
            }

            List<Work> prevCourseWorks = new List<Work>();

            using(HomeServerContext context = new HomeServerContext())
            {
                var prevCourse = context.Courses.Find(courseModel.PrevId);

                if (prevCourse != null)
                {
                    foreach (var work in prevCourse.Works)
                    {
                        var workCopy = new Work()
                        {
                            Name = work.Name,
                            ShortName = work.ShortName,
                            HasThemes = work.HasThemes,
                            WorkTypeId = work.WorkTypeId,
                            WorkControlTypeId = work.WorkControlTypeId,
                            WorkStages = new List<WorkStage>()
                        };

                        prevCourseWorks.Add(workCopy);

                        var tempStages = context.WorkStages.Where(cws => cws.WorkId == work.Id
                            && cws.ParentWorkStageId == null).ToList();

                        var stages = tempStages.Select(ws => new WorkStage() { Id = ws.Id, Name = ws.Name, Optional = ws.Optional})
                            .ToList();

                        for (int i = 0; i < stages.Count; i++)
                        {
                            var workStage = stages[i];

                            var tempSubstages = context.WorkStages
                                .Where(cws => cws.ParentWorkStageId == workStage.Id).ToList();

                            workStage.SubStages = tempSubstages
                                .Select(ws => new WorkStage() { Id = ws.Id, Name = ws.Name, Optional = ws.Optional })
                                .ToList();
                        }

                        workCopy.WorkStages = stages;
                    }
                }
            }

            newCourse = db.Courses.Add(newCourse);
            db.SaveChanges();

            //copy works from previous course

            //if (prevCourse != null)
            //{
                db.SaveChanges();

                foreach (var work in prevCourseWorks)
                {
                    var newWork = new Work()
                    {
                        Course = newCourse,
                        Name = work.Name,
                        ShortName = work.ShortName,
                        HasThemes = work.HasThemes,
                        WorkTypeId = work.WorkTypeId,
                        WorkControlTypeId = work.WorkControlTypeId
                    };

                    db.SaveChanges();
                    newWork = db.Works.Add(newWork);
                    db.SaveChanges();

                    foreach(var workStage in work.WorkStages)
                    {
                        var newWorkStage = new WorkStage()
                        {
                            Name = workStage.Name,
                            Optional = workStage.Optional,
                            Work = newWork
                        };

                        newWorkStage = db.WorkStages.Add(newWorkStage);
                        db.SaveChanges();

                        if (workStage.SubStages != null)
                        {
                            foreach (var subStage in workStage.SubStages)
                            {
                                var newSubStage = new WorkStage()
                                {
                                    Name = subStage.Name,
                                    Optional = subStage.Optional,
                                    ParentStage = newWorkStage,
                                    Work = newWork
                                };

                                db.WorkStages.Add(newSubStage);
                                db.SaveChanges();
                            }
                        }
                    }
                }
            //}

            db.SaveChanges();
            return Request.CreateResponse(HttpStatusCode.OK);
        }
 public WorkThemeCommitService(HomeServerContext context)
 {
     this.context = context;
 }