Esempio n. 1
0
 public IActionResult Save(long applicationId, long environmentId,
                           [FromBody] ApplicationEnvironmentTokenViewModel token)
 {
     token.ApplicationId = applicationId;
     token.EnvironmentId = environmentId;
     return(Ok(_tokenService.Save(token)));
 }
Esempio n. 2
0
        public ApplicationEnvironmentToken Save(ApplicationEnvironmentTokenViewModel toCreate)
        {
            if (!ApplicationExists(toCreate.ApplicationId))
            {
                throw new EntityValidationException("Application does not exist!");
            }
            if (!EnvironmentExists(toCreate.EnvironmentId))
            {
                throw new EntityValidationException("Environment does not exist!");
            }
            if (!ApplicationEnvironmentExists(toCreate.ApplicationId, toCreate.EnvironmentId))
            {
                throw new EntityValidationException("The application isn't configured for this environment.");
            }
            var applicationToken =
                _db.ApplicationTokens.Active()
                .FirstOrDefault(x => x.ApplicationId == toCreate.ApplicationId && x.Name == toCreate.Name);

            if (null == applicationToken)
            {
                throw new EntityValidationException("The application does not have a definition for this token");
            }

            var environmentToken = _db.ApplicationEnvironmentTokens.Active()
                                   .FirstOrDefault(ae => ae.ApplicationTokenId == applicationToken.ApplicationTokenId &&
                                                   ae.ApplicationEnvironment.EnvironmentId == toCreate.EnvironmentId);

            var encryptedValue = _encrypter.Encrypt(toCreate.Value, GetEncryptionKey(toCreate.ApplicationId));

            if (null == environmentToken)
            {
                var applicationEnvironment =
                    _db.ApplicationEnvironments.Active()
                    .First(
                        ae => ae.ApplicationId == toCreate.ApplicationId &&
                        ae.EnvironmentId == toCreate.EnvironmentId);

                environmentToken = new ApplicationEnvironmentToken()
                {
                    ApplicationEnvironment = applicationEnvironment,
                    ApplicationTokenId     = applicationToken.ApplicationTokenId,
                    Value  = encryptedValue,
                    Active = true
                };

                _db.ApplicationEnvironmentTokens.Add(environmentToken);
            }
            else
            {
                environmentToken.Value = encryptedValue;
            }

            _db.SaveChanges();
            return(Get(environmentToken.ApplicationEnvironmentTokenId, true));
        }
        public static ApplicationEnvironmentTokenViewModel ToViewModel(
            this ApplicationEnvironmentToken token)
        {
            var vm = new ApplicationEnvironmentTokenViewModel()
            {
                ApplicationEnvironmentTokenId = token.ApplicationEnvironmentTokenId,
                Name          = token.ApplicationToken.Name,
                Value         = token.Value,
                ApplicationId = token.ApplicationEnvironment.ApplicationId,
                EnvironmentId = token.ApplicationEnvironment.EnvironmentId
            };

            return(vm);
        }