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." }); } }
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); } }
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." }); } }