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

  angular.module('ajenti.settings').run(function(customization) {
    return customization.plugins.settings = {};
  });

}).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, config, core) {
    pageTitle.set('Settings');
    $scope.config = config;
    $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';
      });
    });
    config.load().then(function() {
      return config.getAuthenticationProviders().then(function(p) {
        return $scope.authenticationProviders = p;
      })["catch"](function() {
        return notify.error('Could not load authentication provider list');
      });
    })["catch"](function() {
      return notify.error('Could not load config');
    });
    $scope.$watch('config.data.color', function() {
      if (config.data) {
        return identity.color = config.data.color;
      }
    });
    $scope.save = function() {
      return config.save().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() {
        config.data.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');
          config.data.ssl.enable = true;
          config.data.ssl.certificate = data.path;
          config.data.ssl.client_auth.certificates = [];
          return $scope.save();
        }).error(function(err) {
          return notify.error('Certificate generation failed', err.message);
        });
      });
    };
    $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 config.data.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);
      });
    };
    $scope.addEmail = function(email, username) {
      config.data.auth.emails[email] = username;
      return $scope.newEmailDialogVisible = false;
    };
    $scope.removeEmail = function(email) {
      return delete config.data.auth.emails[email];
    };
    return $scope.restart = function() {
      return core.restart();
    };
  });

}).call(this);

