/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
RED.user = (function() {
function login(opts,done) {
if (typeof opts == 'function') {
done = opts;
opts = {};
}
var dialog = $('
'+
''+
'
'+
''+
'
'+
'
');
dialog.dialog({
autoOpen: false,
dialogClass: "ui-dialog-no-close",
modal: true,
closeOnEscape: !!opts.cancelable,
width: 600,
resizable: false,
draggable: false
});
$("#node-dialog-login-fields").empty();
$.ajax({
dataType: "json",
url: "auth/login",
success: function(data) {
var i=0;
if (data.type == "credentials") {
for (;i",{class:"form-row"});
$(' ').appendTo(row);
var input = $('').appendTo(row);
if (i'+RED._("user.loginFailed")+''+
(opts.cancelable?''+RED._("common.label.cancel")+'':'')+
'').appendTo("#node-dialog-login-fields");
$("#node-dialog-login-submit").button();
$("#node-dialog-login-fields").on("submit", function(event) {
$("#node-dialog-login-submit").button("option","disabled",true);
$("#node-dialog-login-failed").hide();
$(".login-spinner").show();
var body = {
client_id: "node-red-editor",
grant_type: "password",
scope:""
}
for (var i=0;i",{class:"form-row",style:"text-align: center"}).appendTo("#node-dialog-login-fields");
var loginButton = $('',{style: "padding: 10px"}).appendTo(row).on("click", function() {
document.location = field.url;
});
if (field.image) {
$("",{src:field.image}).appendTo(loginButton);
} else if (field.label) {
var label = $('').text(field.label);
if (field.icon) {
$('',{class: "fa fa-2x "+field.icon, style:"vertical-align: middle"}).appendTo(loginButton);
label.css({
"verticalAlign":"middle",
"marginLeft":"8px"
});
}
label.appendTo(loginButton);
}
loginButton.button();
}
}
if (opts.cancelable) {
$("#node-dialog-login-cancel").button().on("click", function( event ) {
$("#node-dialog-login").dialog('destroy').remove();
});
}
var loginImageSrc = data.image || "red/images/node-red-256.png";
$("#node-dialog-login-image").load(function() {
dialog.dialog("open");
}).attr("src",loginImageSrc);
}
});
}
function logout() {
var tokens = RED.settings.get("auth-tokens");
var token = tokens?tokens.access_token:"";
$.ajax({
url: "auth/revoke",
type: "POST",
data: {token:token}
}).done(function(data,textStatus,xhr) {
RED.settings.remove("auth-tokens");
if (data && data.redirect) {
document.location.href = data.redirect;
} else {
document.location.reload(true);
}
}).fail(function(jqXHR,textStatus,errorThrown) {
if (jqXHR.status === 401) {
document.location.reload(true);
} else {
console.log(textStatus);
}
})
}
function updateUserMenu() {
$("#btn-usermenu-submenu li").remove();
if (RED.settings.user.anonymous) {
RED.menu.addItem("btn-usermenu",{
id:"usermenu-item-login",
label:RED._("menu.label.login"),
onselect: function() {
RED.user.login({cancelable:true},function() {
RED.settings.load(function() {
RED.notify(RED._("user.loggedInAs",{name:RED.settings.user.username}),"success");
updateUserMenu();
RED.events.emit("login",RED.settings.user.username);
});
});
}
});
} else {
RED.menu.addItem("btn-usermenu",{
id:"usermenu-item-username",
label:""+RED.settings.user.username+""
});
RED.menu.addItem("btn-usermenu",{
id:"usermenu-item-logout",
label:RED._("menu.label.logout"),
onselect: function() {
RED.user.logout();
}
});
}
}
function init() {
if (RED.settings.user) {
if (!RED.settings.editorTheme || !RED.settings.editorTheme.hasOwnProperty("userMenu")) {
var userMenu = $('
')
.prependTo(".red-ui-header-toolbar");
if (RED.settings.user.image) {
$('').css({
backgroundImage: "url("+RED.settings.user.image+")",
}).appendTo(userMenu.find("a"));
} else {
$('').appendTo(userMenu.find("a"));
}
RED.menu.init({id:"btn-usermenu",
options: []
});
updateUserMenu();
}
}
}
var readRE = /^((.+)\.)?read$/
var writeRE = /^((.+)\.)?write$/
function hasPermission(permission) {
if (permission === "") {
return true;
}
if (!RED.settings.user) {
return true;
}
return checkPermission(RED.settings.user.permissions||"",permission);
}
function checkPermission(userScope,permission) {
if (permission === "") {
return true;
}
var i;
if (Array.isArray(permission)) {
// Multiple permissions requested - check each one
for (i=0;i