Пример #1
0
 public static object GetGradesToGuardian(int evaluationID, int guardianID)
 {
     try
     {
         using (var db = new DBContextModel())
         {
             var availableStudents = BParenting.GetChildren(guardianID);
             List <TblEvaluationStudents> grades = new List <TblEvaluationStudents>();
             availableStudents.ForEach(studentID =>
             {
                 var grade = db.TblEvaluationStudents.Where(x => x.EvaluationFK == evaluationID && x.StudentFK == studentID).FirstOrDefault();
                 if (grade != null)
                 {
                     grades.Add(grade);
                 }
             });
             if (grades.Count() == 0)
             {
                 return(new { result = false, info = "Não existe avaliação." });
             }
             return(new { result = true, data = grades });
         }
     }
     catch (ArgumentException) { return(new { result = false, info = "Não foi encontrada avaliação." }); }
 }
Пример #2
0
 public static List <TblLessonStudents> GetLessonToGuardian(int lessonID, int guardianID)
 {
     try
     {
         using (var db = new DBContextModel())
         {
             var availableStudents             = BParenting.GetChildren(guardianID);
             List <TblLessonStudents> students = new List <TblLessonStudents>();
             availableStudents.ForEach(studentID =>
             {
                 var aux = db.TblLessonStudents.Where(x => x.LessonFK == lessonID && x.StudentFK == studentID).FirstOrDefault();
                 if (aux != null)
                 {
                     students.Add(aux);
                 }
             });
             if (students.Count() == 0)
             {
                 return(null);
             }
             return(students);
         }
     }
     catch (Exception) { return(null); }
 }
Пример #3
0
        public static Boolean SendNotificationToClass(NotificationClass notification, int userID)
        {
            try
            {
                using (var db = new DBContextModel())
                {
                    TblNotifications notif = new TblNotifications
                    {
                        Description = notification.Description,
                        Hour        = DateTime.Now,
                        Subject     = notification.Subject,
                        Urgency     = notification.Urgency,
                        Approval    = notification.Approval,
                        UserFK      = notification.SenderFK
                    };
                    db.TblNotifications.Add(notif);
                    db.SaveChanges();

                    var students = BClass.GetStudentsByClass(notification.ClassFK);
                    foreach (var student in students)
                    {
                        TblValidations valid = new TblValidations
                        {
                            ReceiverFK = BParenting.GetGuardians(student).FirstOrDefault(),
                            StudentFK  = student,
                            Accepted   = false,
                            Read       = false
                        };
                        db.TblValidations.Add(valid);
                        db.SaveChanges();
                    }

                    var cla = db.TblClasses.Find(notification.ClassFK);
                    BAction.SetActionToUser(String.Format("enviou uma notificação a turma '{0}'", cla.Year + cla.ClassDesc), userID);
                    return(true);
                }
            }
            catch (Exception) { return(false); }
        }
Пример #4
0
        public static Object Login(Login requestUser, Uri client)
        {
            try
            {
                using (var db = new DBContextModel())
                {
                    var user = db.TblUsers.Select(x => x).Where(x => x.Email == requestUser.Email).FirstOrDefault();

                    if (user == null || (bool)!user.IsActive)
                    {
                        return(new { result = false, info = "O utilizador não existe ou encontra-se inactivo." });
                    }

                    var password = new PasswordHasher();
                    if (password.VerifyHashedPassword(user.Password, requestUser.Password).ToString() != "Success")
                    {
                        return(new { result = false, info = "O email e a palavra-passe não coincidem." });
                    }

                    byte[] secretKey = Encoding.ASCII.GetBytes("vMDUMFlFl6jUANQZezAu4bAmwBD9IyYl");

                    DateTime issued = DateTime.Now;
                    DateTime expire = DateTime.Now.AddHours(8);
                    var      roles  = db.TblUserRoles.Where(x => x.UserFK == user.ID).Select(x => x.RoleFK).ToList();

                    List <int> classes = new List <int>();
                    classes = db.TblClassUsers.Where(x => x.UserFK == user.ID).Select(x => x.ClassFK).ToList();
                    if (roles.Contains(5))
                    {
                        foreach (int child in BParenting.GetChildren(user.ID))
                        {
                            classes = classes.Concat(db.TblClassUsers.Where(x => x.UserFK == child).Select(x => x.ClassFK)).ToList();
                        }
                    }

                    Dictionary <string, object> payload = new Dictionary <string, object>()
                    {
                        { "iss", client.Authority },
                        { "aud", user.ID },
                        { "iat", _ToUnixTime(issued).ToString() },
                        { "exp", _ToUnixTime(expire).ToString() },
                        { "rol", roles },
                        { "cla", classes }
                    };

                    var token = JWT.Encode(payload, secretKey, JwsAlgorithm.HS256);

                    return(new
                    {
                        result = true,
                        data = new
                        {
                            token = token,
                            userID = user.ID,
                            roles = roles,
                            name = user.Name,
                            photo = user.Photo,
                            classes = classes
                        }
                    });
                }
            }
            catch (Exception) { return(new { result = false, info = "Não foi possível autenticar o utilizador." }); }
        }