Exemple #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();
        }
Exemple #2
0
        public async Task <ActionResult <ValidateResult> > Validate([FromBody] ValidateInput authInput)
        {
            if (this.ModelState.IsValid)
            {
                if (authInput == null)
                {
                    return(BadRequest());
                }

                var signInResult = await _signInManager.PasswordSignInAsync(authInput.Usr, authInput.Pass, true, lockoutOnFailure : false);

                if (signInResult.Succeeded)
                {
                    if (await _usersService.DoesUserHaveAnyActiveDepartments(authInput.Usr))
                    {
                        var user = await _usersService.GetUserByNameAsync(authInput.Usr);

                        Department department = await _departmentsService.GetDepartmentForUserAsync(authInput.Usr);

                        var result = new ValidateResult
                        {
                            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, department.DepartmentId);
                        result.Txd = DateTime.UtcNow.AddMonths(Config.SystemBehaviorConfig.APITokenMonthsTTL)
                                     .ToShortDateString();

                        var profile = await _userProfileService.GetProfileByUserIdAsync(user.Id);

                        result.Nme = profile.FullName.AsFirstNameLastName;

                        return(result);
                    }
                }
            }

            return(BadRequest());
        }