public void ShouldCheckAuthorizeActivityReturnTrue()
        {
            //Arrange
            Person person1 = new Person()
            {
                Id       = 1,
                Name     = "Judas Invisible",
                Username = "******",
                Password = "******",
                RoleId   = 1
            };

            Activity acti = new Activity()
            {
                Id           = 1,
                ActivityName = "Hello",
            };

            RoleDistribution roledis = new RoleDistribution()
            {
                Id         = 1,
                RoleId     = 1,
                ActivityId = 1,
                Activity   = acti
            };

            List <RoleDistribution> listroledis = new List <RoleDistribution>();

            listroledis.Add(roledis);

            UserToken usertok = new UserToken()
            {
                Id        = 1,
                AuthToken = "asdasd",
                Username  = "******",
            };

            //Mock
            mockPersonRepository.Setup(repo => repo.GetPersonByUsername("admin")).Returns(person1);
            mockRoleDistributionRepository.Setup(
                repo => repo.GetAllList(It.IsAny <Expression <Func <RoleDistribution, bool> > >())).Returns(listroledis);
            //Person person = mockPersonRepository.Object.GetPersonByUsername("admin");

            mockRoleDistributionRepository.Setup(repo => repo.GetAllList()).Returns(listroledis);
            //mockActivityReposirity.Setup(repo => repo.Get(1)).Returns(acti);

            //mockUsertokenReposirity.Setup(repo => repo.Get(1)).Returns(usertok);

            ResponseMessageDTO expect = new ResponseMessageDTO(true, "Authorized request");
            //Act

            ResponseMessageDTO output = sutPersonAppservice.CheckAuthorizeActivity("admin", "Hello");

            //ResponseMessageDTO repotest = sutIPersonAppService.CheckAuthorizeActivity("admin", "Hello");

            // Assert

            Assert.AreEqual(expect.Status, output.Status);
        }
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            HttpRequestMessage        request       = actionContext.Request;
            AuthenticationHeaderValue authorization = request.Headers.Authorization;

            if (actionContext.ActionDescriptor.GetCustomAttributes <SkipFilterAttribute>().Any())
            {
                return;
            }

            ActivityAuthorize activityAuthorize = actionContext
                                                  .ActionDescriptor.GetCustomAttributes <ActivityAuthorize>().SingleOrDefault();

            if (activityAuthorize == null || authorization == null)
            {
                actionContext.Response = request.CreateErrorResponse(HttpStatusCode.Unauthorized,
                                                                     _personAppService.Localize("Unauthorized_Request_NoUser"));
                return;
            }

            string userAuthToken = authorization.ToString();
            // Authentication by validate user token
            ResponseMessageDTO tokenResponseMessageDto = _tokenAppService.ValidateToken(userAuthToken);

            if (!tokenResponseMessageDto.Status)
            {
                actionContext.Response = request.CreateErrorResponse(HttpStatusCode.Unauthorized,
                                                                     tokenResponseMessageDto.Message);
                return;
            }
            else
            {
                string username = _tokenAppService.GetUsernameByToken(userAuthToken);

                if (string.IsNullOrEmpty(activityAuthorize.ActivityName))
                {
                    return;
                }
                ResponseMessageDTO authorizeActivityResponseMessageDto = _personAppService
                                                                         .CheckAuthorizeActivity(username, activityAuthorize.ActivityName);
                if (!authorizeActivityResponseMessageDto.Status)
                {
                    actionContext.Response = request.CreateErrorResponse(HttpStatusCode.Unauthorized,
                                                                         authorizeActivityResponseMessageDto.Message);
                    return;
                }
            }
        }
 public static bool TryParseResponseMessageDTO(this ActivityResponseDTO activityResponse, out ResponseMessageDTO responseMessageDTO)
 {
     return(TryParseBaseDTO <ResponseMessageDTO>(activityResponse, ResponseMessagePropertyName, out responseMessageDTO));
 }
 public static ActivityResponseDTO AddResponseMessageDTO(this ActivityResponseDTO activityResponse, ResponseMessageDTO responseMessageDTO)
 {
     return(AddBaseDTO <ResponseMessageDTO>(activityResponse, ResponseMessagePropertyName, responseMessageDTO));
 }
예제 #5
0
        /// <summary>
        /// Maintain all trainings in database. Auto update Training's information when its reach the start date or end date
        /// </summary>
        private void MaintainTrainings(object state)
        {
            try
            {
                //Set state of system to true -> no one can access system when its maintain
                SystemState.SetSystemState(true);

                List <int> listOnGoingTrainings   = new List <int>();
                List <int> listCompletedTrainings = new List <int>();

                var trainings = _trainingRepository.GetAll();

                foreach (Training training in trainings.ToList())
                {
                    #region Get array of trainings that going to start today

                    if (DateTime.Now >= training.StartDate && training.StatusId == 4)
                    {
                        listOnGoingTrainings.Add(training.Id);
                    }

                    #endregion

                    #region Get array of trainings that going to completed today

                    if (DateTime.Now > training.EndDate && training.StatusId == 3)
                    {
                        listCompletedTrainings.Add(training.Id);
                    }

                    #endregion
                }

                #region Update trainings which start today

                foreach (int trainingID in listOnGoingTrainings)
                {
                    var training = _trainingRepository.Get(trainingID);

                    training.StatusId = 4;

                    training = _trainingRepository.Update(training);
                }

                #endregion

                #region Update trainings which completed today

                foreach (int trainingID in listCompletedTrainings)
                {
                    #region Get array of trainees who was assigned to this training and update their information

                    List <TraineeTrainingOutcomeDTO> listTraineesAssigned = new List <TraineeTrainingOutcomeDTO>();

                    var training = _trainingRepository.Get(trainingID);

                    #region Get competencies trained by this training

                    List <int> competenciesTrainedID = new List <int>();

                    //Get program by programID
                    var program = _programRepository.Get(training.ProgramId);

                    int[] modulesIncludedID = program.ArrayOfIncludedModules;

                    //Get list module by array moduleID
                    var modulesIncluded = _moduleRepository.GetModulesByArrayID(modulesIncludedID);

                    foreach (Module module in modulesIncluded)
                    {
                        //Get list competencies trained by module
                        List <int> competenciesTrainedIDByModule = module.ArrayOfTrainingCompetencies.ToList();

                        //Add there competencies to list competencies trained by program
                        foreach (int competenceID in competenciesTrainedIDByModule)
                        {
                            competenciesTrainedID.Add(competenceID);
                        }
                    }

                    //Remove the duplicate competencies
                    competenciesTrainedID = competenciesTrainedID.Distinct().ToList();

                    #endregion

                    #region Get trainees ID represent the trainees who was assigned to this training

                    List <int> listTraineesIDAssigned = training.ArrayOfAssignedTrainees.ToList();

                    #endregion

                    #region Add the trainees assigned to the list of trainees assigned above

                    foreach (int traineeID in listTraineesIDAssigned)
                    {
                        TraineeTrainingOutcomeDTO traineeTrainingOutcomeDto = new TraineeTrainingOutcomeDTO();

                        traineeTrainingOutcomeDto.TraineeId            = traineeID;
                        traineeTrainingOutcomeDto.SelectedCompetencies = competenciesTrainedID.ToArray();

                        listTraineesAssigned.Add(traineeTrainingOutcomeDto);
                    }

                    #endregion

                    #region Update training and trainees assigned information

                    //Convert list trainees assigned to JSON string
                    string JSONTraineesAssigned = JsonConvert.SerializeObject(listTraineesAssigned.ToArray());

                    ResponseMessageDTO updateCompleted = CompleteTraining(new CompleteTrainingInputDTO(trainingID, JSONTraineesAssigned));

                    #endregion

                    #endregion
                }

                #endregion

                System.Threading.Thread.Sleep(5 * 60 * 1000);

                //When the maintain is over, set state of system to false -> people can access system normally
                SystemState.SetSystemState(false);
            }
            catch
            {
                SystemState.SetSystemState(false);
            }
        }