Welcome to mirror list, hosted at ThFree Co, Russian Federation.

giveViewAccess.js « javascripts « UsersManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91300547c7cb95cb3b07e39878c96277031862d7 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*!
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

$(document).ready(function () {

    function hideLoading()
    {
        $('#giveUserAccessToViewReports').prop('disabled', false);
        $('#ajaxLoadingGiveViewAccess').hide();
    }

    function showLoading()
    {
        $('#giveUserAccessToViewReports').prop('disabled', true);
        $('#ajaxLoadingGiveViewAccess').show();
    }

    function showErrorNotification(errorMessage)
    {
        var placeAt = '#ajaxErrorGiveViewAccess';
        $(placeAt).show();

        var UI = require('piwik/UI');
        var notification = new UI.Notification();
        notification.show(errorMessage, {
            placeat: placeAt,
            context: 'error',
            id: 'ajaxHelper',
            type: null
        });
        notification.scrollToNotification();
        hideLoading();
    }

    function createNewAjaxHelper()
    {
        var ajaxHandler = new ajaxHelper();
        ajaxHandler.setCompleteCallback(function (xhr, status) {
            if (xhr &&
                xhr.responseJSON &&
                xhr.responseJSON.message &&
                xhr.responseJSON.result &&
                xhr.responseJSON.result == 'error') {
                hideLoading();
            }
            if (status && String(status).toLowerCase() !== 'sucess') {
                hideLoading();
            }
        });
        ajaxHandler.addParams({
            module: 'API',
            format: 'json'
        }, 'GET');
        ajaxHandler.setErrorElement('#ajaxErrorGiveViewAccess');

        return ajaxHandler;
    }

    function sendViewAccess(userLogin)
    {
        sendUpdateUserAccess(userLogin, 'view', function () { window.location.reload(); });
        setTimeout(hideLoading, 250);
        // we hide loading after a bit since we cannot influence the ajax request in case of any error
    }

    function setViewAccessForUserToAllWebsitesIfUserConfirms(userLogin)
    {
        // ask confirmation
        $('#confirm').find('#login').text(userLogin);

        function onValidate() {
            sendViewAccess(userLogin);
        }

        piwikHelper.modalConfirm('#confirm', {yes: onValidate, no: hideLoading})
    }

    function setViewAccessForUserIfNotAlreadyHasAccess(userLogin, idSites)
    {
        var ajaxHandler = createNewAjaxHelper();
        ajaxHandler.addParams({
            method: 'UsersManager.getUsersAccessFromSite',
            userLogin: userLogin,
            idSite: idSites
        }, 'GET');
        ajaxHandler.setCallback(function (users) {
            if (users && users[0] && users[0][userLogin]) {
                showErrorNotification(_pk_translate('UsersManager_ExceptionUserHasViewAccessAlready'));
            } else {
                sendViewAccess(userLogin);
            }

        });
        ajaxHandler.send();
    }

    function ifUserExists(usernameOrEmail, callback)
    {
        var ajaxHandler = createNewAjaxHelper();
        ajaxHandler.addParams({
            method: 'UsersManager.userExists',
            userLogin: usernameOrEmail,
        }, 'GET');
        ajaxHandler.setCallback(callback);
        ajaxHandler.send();
    }

    function getUsernameFromEmail(usernameOrEmail, callback)
    {
        var ajaxHandler = createNewAjaxHelper();
        ajaxHandler.addParams({
            method: 'UsersManager.getUserLoginFromUserEmail',
            userEmail: usernameOrEmail,
        }, 'GET');
        ajaxHandler.setCallback(callback);
        ajaxHandler.send();
    }

    function giveViewAccessToUser(userLogin)
    {
        var idSites = getIdSites();

        if (idSites === 'all') {
            setViewAccessForUserToAllWebsitesIfUserConfirms(userLogin);
        } else {
            setViewAccessForUserIfNotAlreadyHasAccess(userLogin, idSites);
        }
    }

    $('#showGiveViewAccessForm').click(function () {
        $('#giveViewAccessForm').toggle()
    });

    $('#giveViewAccessForm #user_invite').keypress(function (e) {
        var key = e.keyCode || e.which;
        if (key == 13) {
            $('#giveViewAccessForm #giveUserAccessToViewReports').click();
        }
    });

    $('#giveViewAccessForm #giveUserAccessToViewReports').click(function () {
        showLoading();

        var usernameOrEmail = $('#user_invite').val();

        if (!usernameOrEmail) {
            showErrorNotification(_pk_translate('UsersManager_ExceptionNoValueForUsernameOrEmail'));
            return;
        }

        ifUserExists(usernameOrEmail, function (isUserName) {
            if (isUserName && isUserName.value) {
                giveViewAccessToUser(usernameOrEmail);
            } else {
                getUsernameFromEmail(usernameOrEmail, function (login) {
                    if (login && login.value) {
                        giveViewAccessToUser(login.value);
                    } else {
                        hideLoading();
                    }
                });
            }
        });
    });
});