diff --git a/libresapi/src/api/ServiceControlHandler.cpp b/libresapi/src/api/ServiceControlHandler.cpp index 84497f5e5..f426f3378 100644 --- a/libresapi/src/api/ServiceControlHandler.cpp +++ b/libresapi/src/api/ServiceControlHandler.cpp @@ -46,6 +46,7 @@ ServiceControlHandler::ServiceControlHandler(RsServiceControl* control): mRsServiceControl(control) { addResourceHandler("*", this, &ServiceControlHandler::handleWildcard); + addResourceHandler("user", this, &ServiceControlHandler::handleUser); } void ServiceControlHandler::handleWildcard(Request &req, Response &resp) @@ -87,7 +88,7 @@ void ServiceControlHandler::handleWildcard(Request &req, Response &resp) //uint32_t serviceid = fromString(serviceidtext); uint32_t serviceid = atoi(serviceidtext.c_str()); if (serviceid == 0) { - resp.setFail("serviceid missed"); + resp.setFail("service_id missed"); return; } @@ -123,4 +124,62 @@ void ServiceControlHandler::handleWildcard(Request &req, Response &resp) } } + +void ServiceControlHandler::handleUser(Request& req, Response& resp){ + // no get, only put (post) to allow user or delete to remove user + + std::string serviceidtext; + std::string peeridtext; + bool enabled; + bool ok; + + req.mStream << makeKeyValueReference("service_id", serviceidtext) + << makeKeyValueReference("peer_id", peeridtext) + << makeKeyValueReference("enabled", enabled); + + RsPeerId peer_id(peeridtext); + + if (peer_id.isNull()) { + resp.setFail("peer_id missing or not found"); + return; + } + + RsServicePermissions serv_perms ; + uint32_t serviceid = atoi(serviceidtext.c_str()); + if (serviceid == 0) { + resp.setFail("service_id missed"); + return; + } + + if(!rsServiceControl->getServicePermissions(serviceid, serv_perms)){ + resp.setFail("service_id " + serviceidtext + " is invalid"); + return; + } + + if(req.isPut()) + { + if (enabled && !serv_perms.peerHasPermission(peer_id)) + { + serv_perms.setPermission(peer_id); + } else if (!enabled && serv_perms.peerHasPermission(peer_id)){ + serv_perms.resetPermission(peer_id); + } else { + //nothing todo + resp.setOk(); + return; + } + + } else { + resp.setFail("only POST supported."); + return; + } + ok = rsServiceControl->updateServicePermissions(serviceid,serv_perms); + if (!ok) { + resp.setFail("updateServicePermissions failed"); + return; + } + + resp.setOk(); +} + } // namespace resource_api diff --git a/libresapi/src/api/ServiceControlHandler.h b/libresapi/src/api/ServiceControlHandler.h index cead8e79a..da1db64e4 100644 --- a/libresapi/src/api/ServiceControlHandler.h +++ b/libresapi/src/api/ServiceControlHandler.h @@ -16,5 +16,6 @@ public: private: RsServiceControl* mRsServiceControl; void handleWildcard(Request& req, Response& resp); + void handleUser(Request& req, Response& resp); }; } // namespace resource_api diff --git a/libresapi/src/webui-src/app/accountselect.js b/libresapi/src/webui-src/app/accountselect.js index 1b989579c..649c89d6a 100644 --- a/libresapi/src/webui-src/app/accountselect.js +++ b/libresapi/src/webui-src/app/accountselect.js @@ -16,6 +16,7 @@ function selAccount(account){ m.redraw(); rs.request("control/login", {id: curraccount.id}, function(){ console.log("login sent"); + rs.clearCache(); }); } diff --git a/libresapi/src/webui-src/app/peers.js b/libresapi/src/webui-src/app/peers.js index 76ff118a8..6d082be60 100644 --- a/libresapi/src/webui-src/app/peers.js +++ b/libresapi/src/webui-src/app/peers.js @@ -8,7 +8,7 @@ module.exports = {view: function(){ //console.log("peers:" + peers); //waiting for peerlist ... - if(peers === undefined){ + if(peers === undefined || peers == null){ return m("div",[ m("h2","peers"), m("h3","waiting_server"), @@ -31,14 +31,14 @@ module.exports = {view: function(){ if (location.avatar_address != "" && avatar_address =="") { avatar_address=location.avatar_address; } - return m("div",{ + return m("li",{ style:"color:" + (location.is_online ? "lime": "grey") + ";cursor:pointer", onclick: function(){ m.route("/chat?lobby=" + location.chat_id) } - },location.location); + }, location.location); }); //return friend (peer + locations) @@ -58,7 +58,7 @@ module.exports = {view: function(){ {style:"color:" + (isonline ? "lime": "grey")} , peer.name ), - m("div", loclist ), + m("ul", loclist ), ]), //remove-button m("div", { diff --git a/libresapi/src/webui-src/app/retroshare.js b/libresapi/src/webui-src/app/retroshare.js index be1213412..4ebb221bc 100644 --- a/libresapi/src/webui-src/app/retroshare.js +++ b/libresapi/src/webui-src/app/retroshare.js @@ -261,6 +261,12 @@ rs.forceUpdate = function(path, removeCache){ } } +// force reload for all +rs.clearCache = function(path, removeCache){ + cache = {}; +} + + //return api-path rs.apiurl = function(path) { if (path === undefined) { @@ -372,3 +378,19 @@ rs.sort.bool = function(name, innersort){ } } } + +// searching a element in a list +// items: list to search in +// name: name of attribute to lookup +// value: attribute's value to compare +rs.find = function(items, name, value) { + if (items === undefined||items == null) { + return null; + }; + for(var i = 0, l = items.length; i < l; ++i) { + if (items[i][name] == value) { + return items[i]; + } + } + return null; +} diff --git a/libresapi/src/webui-src/app/servicecontrol.js b/libresapi/src/webui-src/app/servicecontrol.js index 9d4b36321..faf964bd8 100644 --- a/libresapi/src/webui-src/app/servicecontrol.js +++ b/libresapi/src/webui-src/app/servicecontrol.js @@ -13,8 +13,150 @@ function setOption(id,value) { } } +function setUserOption(serviceid, userid, value) { + return function(){ + rs.request("servicecontrol/user", { + service_id: serviceid, + peer_id: userid, + enabled: value + }, function(){ + rs.forceUpdate("servicecontrol", true) + }); + } +} + +function createSwitch(isOn, width) { + if (width === undefined) { + width = "2.1em"; + } + return [ + m("div.menu", { + style: { + float:"left", + width: width, + color: "#303030", + textAlign: "center", + backgroundColor: !isOn + ? "black" + : "lime", + } + }, "ON"), + m("div.menu",{ + style: { + float:"left", + width: width, + textAlign: "center", + marginRight:"5px", + color: "#303030", + backgroundColor: isOn + ? "black" + : "lime", + } + }, "OFF"), + ]; +} + +function serviceView(serviceid) { + var service, liste; + service = rs.find(rs("servicecontrol"),"service_id",serviceid); + if (service == null) { + return m("h3",""); + } + liste = service.default_allowed + ? service.peers_denied + : service.peers_allowed; + return m("div", [ + m("h2","Options / Rights / " + service.service_name), + m("div.btn2",{ + onclick: function(){ + m.route("/options/servicecontrol") + }, + },"back to options / rights"), + m("hr"), + m("h2",{ + style:{ + float:"left", + } + },[ + m("div",{ + style:{ + float:"left", + } + },"user rights for: " + service.service_name + ", default: "), + m("div", { + onclick: setOption( + serviceid, + !service.default_allowed + ), + style: { + float:"left", + marginLeft: "0.4em", + marginRight: "0.4em", + } + },createSwitch(service.default_allowed)), + ]), + m("div", { + style: { + clear:"left", + } + }), + m("ul", rs.list("peers",function(peer){ + var locs; + locs = peer.locations; + locs.sort(rs.sort("location")); + return peer.locations.map(function(location){ + var isExcept, isOn; + isExcept = liste != null + && liste.indexOf(location.peer_id)>=0; + isOn = service.default_allowed ? !isExcept: isExcept; + return m("li", { + style: { + margin: "5px", + color: isOn ? "lime" :"red", + } + }, [ + m("div"), + m("div", { + onclick: setUserOption( + serviceid, + location.peer_id, + !isOn + ), + style: { + float:"left", + }, + },createSwitch(isOn)), + m("div", + { + style: { + color: "lime", + float:"left", + marginLeft: "5px", + marginRight: "5px", + fontWeight: "bold", + } + }, + peer.name + (location.location + ? " (" + location.location + ")" + : "") + ), + m("div", { + style: { + clear: "left" + } + }), + ]); + }) + }, rs.sort("name"))) + ]); +} + + module.exports = { view: function(){ + if (m.route.param("service_id")) { + return serviceView(m.route.param("service_id")); + } return m("div", [ m("h2","Options / Rights"), m("div.btn2",{ @@ -24,51 +166,70 @@ module.exports = { },"back to options"), m("hr"), m("ul", rs.list("servicecontrol", function(item){ - //return m("li",item.service_name) - if (item.service_name.match("banlist")) { - console.log("banlist:" + item.default_allowed); - } return m("li", { style: { - margin: "2px", + margin: "5px", color: item.default_allowed ? "lime" :"red", } }, [ + m("div"), m("div", { onclick: setOption( item.service_id, !item.default_allowed ), - }, [ - m("div.menu", { - style: { - float:"left", - width:"30px", - color: "#303030", - textAlign: "center", - backgroundColor: !item.default_allowed - ? "black" - : "lime", - } - }, "ON"), - m("div.menu",{ - style: { - float:"left", - width:"30px", - textAlign: "center", - marginRight:"5px", - color: "#303030", - backgroundColor: item.default_allowed - ? "black" - : "lime", - } - }, "OFF"), - ]), - m("div", { style: { - color: "lime", + float:"left", } - }, item.service_name), + },createSwitch(item.default_allowed)), + m("div.menu", + { + style: { + color: "lime", + float: "left", + marginLeft: "5px", + marginRight: "5px", + paddingLeft: "2px", + paddingRight: "2px", + }, + onclick: function(){ + m.route("/options/servicecontrol/", { + service_id: item.service_id, + }) + } + }, "more" + ), + m("div", + { + style: { + color: "lime", + float:"left", + marginLeft: "5px", + marginRight: "5px", + fontWeight: "bold", + } + }, + item.service_name + ), + m("div", + { + style: { + color: "lime", + float:"left", + marginLeft: "5px", + marginRight: "5px", + } + }, + ( + item.default_allowed + ? ( item.peers_denied != null + ? "(" + item.peers_denied.length + " denied)" + : "") + : ( item.peers_allowed != null + ? "(" + item.peers_allowed.length + " allowed)" + : "") + ) + ), m("div", { style: { clear: "left"