// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('ajenti.settings', ['core']);

}).call(this);

// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('core').config(function($routeProvider) {
    return $routeProvider.when('/view/settings', {
      templateUrl: '/settings:resources/partial/index.html',
      controller: 'SettingsIndexController'
    });
  });

}).call(this);

// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('ajenti.settings').controller('SettingsIndexController', function($scope, $http, $sce, notify, pageTitle, identity, messagebox, passwd, settings) {
    pageTitle.set('Settings');
    $scope.availableColors = ['default', 'bluegrey', 'red', 'deeporange', 'orange', 'green', 'teal', 'blue', 'purple'];
    $scope.newClientCertificate = {
      c: 'NA',
      st: 'NA',
      o: '',
      cn: ''
    };
    identity.promise.then(function() {
      $scope.newClientCertificate.o = identity.machine.name;
      return passwd.list().then(function(data) {
        $scope.availableUsers = data;
        $scope.$watch('newClientCertificate.user', function() {
          return $scope.newClientCertificate.cn = "" + identity.user + "@" + identity.machine.hostname;
        });
        return $scope.newClientCertificate.user = 'root';
      });
    });
    settings.getConfig().then(function(data) {
      return $scope.config = data;
    })["catch"](function() {
      $scope.config = {};
      return notify.error('Could not load config');
    });
    $scope.$watch('config.color', function() {
      if ($scope.config) {
        return identity.color = $scope.config.color;
      }
    });
    $scope.save = function() {
      return settings.setConfig($scope.config).then(function(data) {
        return notify.success('Saved');
      })["catch"](function() {
        return notify.error('Could not save config');
      });
    };
    $scope.createNewServerCertificate = function() {
      return messagebox.show({
        title: 'Self-signed certificate',
        text: 'Generating a new certificate will void all existing client authentication certificates!',
        positive: 'Generate',
        negative: 'Cancel'
      }).then(function() {
        $scope.config.ssl.client_auth.force = false;
        notify.info('Generating certificate', 'Please wait');
        return $http.get('/api/settings/generate-server-certificate').success(function(data) {
          notify.success('Certificate successfully generated');
          $scope.config.ssl.enable = true;
          $scope.config.ssl.certificate = data.path;
          $scope.config.ssl.client_auth.certificates = [];
          return $scope.save();
        }).error(function(err) {
          return notify.error('Certificate generation failed', err.message);
        });
      });
    };
    return $scope.generateClientCertificate = function() {
      $scope.newClientCertificate.generating = true;
      return $http.post('/api/settings/generate-client-certificate', $scope.newClientCertificate).success(function(data) {
        $scope.newClientCertificate.generating = false;
        $scope.newClientCertificate.generated = true;
        $scope.newClientCertificate.url = $sce.trustAsUrl("data:application/x-pkcs12;base64," + data.b64certificate);
        return $scope.config.ssl.client_auth.certificates.push({
          user: $scope.newClientCertificate.user,
          digest: data.digest,
          name: data.name,
          serial: data.serial
        });
      }).error(function(err) {
        $scope.newClientCertificate.generating = false;
        $scope.newClientCertificateDialogVisible = false;
        return notify.error('Certificate generation failed', err.message);
      });
    };
  });

}).call(this);

// Generated by CoffeeScript 1.8.0
(function() {
  angular.module('ajenti.settings').service('settings', function($http, $q) {
    this.getConfig = function() {
      var q;
      q = $q.defer();
      $http.get("/api/settings/config").success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    this.setConfig = function(config) {
      var q;
      q = $q.defer();
      $http.post("/api/settings/config", config).success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    this.getUserConfig = function() {
      var q;
      q = $q.defer();
      $http.get("/api/settings/user-config").success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    this.setUserConfig = function(config) {
      var q;
      q = $q.defer();
      $http.post("/api/settings/user-config", config).success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    return this;
  });

}).call(this);

