summaryrefslogtreecommitdiff
path: root/www/js/auth.js
blob: aee850267ab16d813d80dfc3038fa0e6fcc9d813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 *  rhwebimport
 *
 *  Copyright (C) 2014-2016 Christian Pointner <equinox@helsinki.at>
 *  Copyright (C) 2015-2016 Peter Grassberger <petertheone@gmail.com>
 *
 *  This file is part of rhwebimport.
 *
 *  rhwebimport is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  rhwebimport is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with rhwebimport. If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

var Rivendell = Rivendell || {};

var auth_username = null;
var auth_fullname = null;
var auth_token = null;

function auth_loginSuccess(data) {
   if (data.status == 'OK') {
     auth_username = data.username;
     auth_fullname = data.fullname;
     auth_token = data.token;

     sessionStorage.setItem("auth_username", auth_username);
     sessionStorage.setItem("auth_fullname", auth_fullname);
     sessionStorage.setItem("auth_token", auth_token);

     // todo: do this at a central place
     rdxport = new Rivendell.Rdxport(auth_username, auth_token, '/rd-bin/rdxport.cgi');
     rdxport.setListDropboxesEndpoint('/rh-bin/listdropboxes.cgi');
     rdxport.setMusicgridEndpoint('/rh-bin/musicgrid.cgi');

     apps_select(apps_current);

     $('#username-field').html(auth_fullname + ' (' + auth_username + ')');
     $('#loginbox').slideUp();
     $('#mainwindow').fadeIn();
   } else {
     alertbox.error('loginbox', "Fehler beim Login", data.errorstring);
     auth_cleanup();
   }
}

function auth_loginError(req, status, error) {
   var message = req.status + ': ' + error;
   if(req.status == 401) {
     message = "Benutzer und/oder Passwort sind falsch!";
   }
   alertbox.error('loginbox', "Fehler beim Login", message);
   $("#password").val('');
}

function auth_logout() {
  auth_cleanup();
  apps_cleanup();

  $(".alert").alert('close');
  $("#username").val('');
  $("#password").val('');
  $("#mainwindow").fadeOut();
  $('#username-field').html('');
  $('#loginbox').slideDown();
}

function auth_init() {
  auth_username = sessionStorage.getItem("auth_username");
  auth_fullname = sessionStorage.getItem("auth_fullname");
  auth_token = sessionStorage.getItem("auth_token");

  if(auth_token && auth_username && auth_fullname) {
    $("#loginbox").hide();
    $('#username-field').html(auth_fullname + ' (' + auth_username + ')');
  } else {
    $("#mainwindow").hide();
  }
  $("#loginform").submit(function(event) {
    event.preventDefault();

    Rivendell.Rdxport.authLogin(
        '/rh-bin/authtoken.json',
        $("#username").val(),
        $("#password").val(),
        auth_loginSuccess
    ).fail(auth_loginError);
  });
}

function auth_cleanup() {
  sessionStorage.removeItem("auth_username");
  sessionStorage.removeItem("auth_fullname");
  sessionStorage.removeItem("auth_token");

  auth_username = null;
  auth_fullname = null;
  auth_token = null;

  $("#username").val('').focus();
  $("#password").val('');
}