Exemplo n.º 1
0
        public async Task <IActionResult> UserActivation(UserActivationDto model)
        {
            try
            {
                var activationResult = await _userManagerService.UserActivation(model);

                if (activationResult)
                {
                    var response = new ResponseDataObject
                    {
                        Message = $"User with {model.UserId} activated successfully",
                        Status  = activationResult,
                    };
                    return(Ok(response));
                }
                return(Unauthorized());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
Exemplo n.º 2
0
        public async Task <bool> UserActivation(UserActivationDto activationDto)
        {
            using (var _activateTrans = _userProfileDbContext.Database.BeginTransaction())
            {
                var activate = await _userProfileDbContext.UserActivationModel.FirstOrDefaultAsync(a => a.ActivationCode == activationDto.ActivationCode &&
                                                                                                   a.UserModelId == activationDto.UserId);

                if (activate != null)
                {
                    //check if code has expired
                    var checkTime = activate.ExpiresAt >= (DateTime.UtcNow) ? true : false;
                    if (checkTime)
                    {
                        //delete the code from the database
                        _userProfileDbContext.Remove(activate);
                        await _userProfileDbContext.SaveChangesAsync();

                        //update user table with statuses
                        var user = await _userProfileDbContext.UserModel.FirstOrDefaultAsync(u => u.Id == activationDto.UserId);

                        if (user != null)
                        {
                            user.IsActivated = true;
                            user.IsActive    = true;

                            _userProfileDbContext.Entry(user).State = EntityState.Modified;
                            await _userProfileDbContext.SaveChangesAsync();
                        }

                        //commit to database
                        _activateTrans.Commit();

                        return(true);
                    }
                    return(false);
                }
            }
            return(false);
        }