Exemplo n.º 1
0
        public ActiveCompanyResult ValidateForDepartment([FromBody] SetActiveComapnyInput authInput)
        {
            if (this.ModelState.IsValid)
            {
                if (authInput == null)
                {
                    throw HttpStatusCode.BadRequest.AsException();
                }

                // Hack while services is migrated to DotNetCore and can utilize the underlying calls
                var client  = new RestClient(Config.SystemBehaviorConfig.ResgridBaseUrl);
                var request = new RestRequest($"/CoreBridge/ValidateLogIn", Method.POST);
                request.AddJsonBody(authInput);
                var response = client.Execute <Model.Results.ValidateLogInResult>(request);

                if (response.Data == null || !response.Data.Successful)
                {
                    throw HttpStatusCode.Unauthorized.AsException();
                }

                var user = _usersService.GetUserByName(authInput.Usr);

                if (_departmentsService.IsMemberOfDepartment(authInput.Did, user.Id))
                {
                    Department department = _departmentsService.GetDepartmentForUser(authInput.Usr);

                    var result = new ActiveCompanyResult
                    {
                        Eml = user.Email,
                        Uid = user.Id,
                        Dnm = department.Name,
                        Did = department.DepartmentId
                    };

                    if (department.CreatedOn.HasValue)
                    {
                        result.Dcd = (department.CreatedOn.Value - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds.ToString();
                    }
                    else
                    {
                        result.Dcd = new DateTime(1970, 1, 1).ToLocalTime().ToString();
                    }

                    result.Tkn = V3AuthToken.Create(authInput.Usr, authInput.Did);
                    result.Txd = DateTime.UtcNow.AddMonths(Config.SystemBehaviorConfig.APITokenMonthsTTL).ToShortDateString();

                    var profile = _userProfileService.GetProfileByUserId(user.Id);
                    result.Nme = profile.FullName.AsFirstNameLastName;

                    return(result);
                }

                throw HttpStatusCode.Unauthorized.AsException();
            }

            throw HttpStatusCode.BadRequest.AsException();
        }