コード例 #1
0
        public IHttpActionResult GetByUserName([FromUri] string username)
        {
            TeacherDto teacher = new TeacherDto();

            try
            {
                teacher = db.GetByUserName(username);
            }
            catch (NullReferenceException e)
            {
                logger.Error(e.Message);
                return(NotFound());
            }

            return(Ok(teacher));
        }
コード例 #2
0
        public HttpResponseMessage GetTeacherByUserName(string username)
        {
            string userId   = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == "UserId").Value;
            string userRole = ((ClaimsPrincipal)RequestContext.Principal).FindFirst(x => x.Type == ClaimTypes.Role).Value;

            logger.Info("UserRole: " + userRole + ", UserId: " + userId + ": Requesting Teacher by username: "******"The teacher with username: "******" was not found.");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "The teacher with username: "******" was not found."));
                }
                if (userRole == "admin")
                {
                    logger.Info("Requesting found teacher convert for " + userRole + "role.");
                    TeacherDTOForAdmin dto = toDTO.ConvertToTeacherDTOForAdmin(teacher, (List <IdentityUserRole>)teacher.Roles);
                    if (dto == null)
                    {
                        logger.Info("Failed!");
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong."));
                    }
                    logger.Info("Success!");
                    return(Request.CreateResponse(HttpStatusCode.OK, dto));
                }
                else if (userRole == "teacher" && teacher.IsStillWorking == true)
                {
                    logger.Info("Requesting found teacher convert for " + userRole + "role.");
                    TeacherDTOForTeacher dto = toDTO.ConvertToTeacherDTOForTeacher(teacher);
                    if (dto == null)
                    {
                        logger.Info("Failed!");
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong."));
                    }
                    logger.Info("Success!");
                    return(Request.CreateResponse(HttpStatusCode.OK, dto));
                }
                else if (teacher.IsStillWorking == true && (userRole == "student" || userRole == "parent"))
                {
                    logger.Info("Requesting found teacher convert for " + userRole + "role.");
                    TeacherDTOForStudentAndParent dto = toDTO.ConvertToTeacherDTOForStudentAndParent(teacher);
                    if (dto == null)
                    {
                        logger.Info("Failed!");
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Something went wrong."));
                    }
                    logger.Info("Success!");
                    return(Request.CreateResponse(HttpStatusCode.OK, dto));
                }
                else
                {
                    logger.Info("Authorisation failure. User " + userId + " is not authorised for this request.");
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Access Denied. " +
                                                       "We’re sorry, but you are not authorized to perform the requested operation."));
                }
            }
            catch (Exception e)
            {
                logger.Error(e);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }