Security 用户如何登录到定义了读卡器角色的couchapp?

Security 用户如何登录到定义了读卡器角色的couchapp?,security,couchdb,couchapp,Security,Couchdb,Couchapp,我通过Couchapp部署了我的应用程序,这意味着整个应用程序都是从数据库中提供的。我不希望Couchdb数据库中的数据公开,所以我指定了一个读者角色,用户必须拥有这个角色,然后才能为其提供数据。然而,当我进入应用程序时,我能得到的只是: {"error":"unauthorized","reason":"You are not authorized to access this db."} 因为它甚至不能提供使用jquery.coach.js的登录页面 关于如何提供应用内登录(即,对于需要数

我通过Couchapp部署了我的应用程序,这意味着整个应用程序都是从数据库中提供的。我不希望Couchdb数据库中的数据公开,所以我指定了一个读者角色,用户必须拥有这个角色,然后才能为其提供数据。然而,当我进入应用程序时,我能得到的只是:

{"error":"unauthorized","reason":"You are not authorized to access this db."}
因为它甚至不能提供使用jquery.coach.js的登录页面


关于如何提供应用内登录(即,对于需要数据读取访问的用户,不使用Futon登录)有什么想法吗?

目前,解决方案需要一些工作。(社会上有压力要求改进这一点,但我将解释目前的答案,而不是描述提案或空谈。)

制作一个“welcome mat”数据库,具有以下功能:

  • 拥有管理员用户(“jtsnake”):
    \u security.admins={“名称”:[“jtsnake”],“角色”:[]}
  • 公开可读:
    \u security.readers={“名称”:[],“角色”:[]}
  • 具有带有
    .validate\u doc\u update
    功能的设计文档。不允许任何更改,管理员除外:

    function(newDoc, oldDoc, userCtx, secObj) {
        // _design/welcome_mat .validate_doc_update
    
        if(! userCtx.name)
          throw {"unauthorized": "Please log in to change the welcome mat"};
        if(userCtx.roles.indexOf("_admin") === -1)
          throw {"forbidden": "Only the admin can change the welcome mat"};
        log("Allowing welcome mat update by: " + userCtx.name);
    }
    
  • 最后,将您的公共内容(如欢迎屏幕、登录屏幕等)放置在此数据库中。一旦用户登录,私有数据就可以进入私有数据库


对于您和其他人只能直接与此应用程序的用户交谈(如个人或内部事务)的特定情况,一个有效的快速解决方案是创建用户登录表单、建立会话并刷新页面:

var form = '<div style="position: absolute; left: 50%; top: 50%; margin-left: -150px; margin-top: -100px; width:3 00px; height: 200px;">'
         + '  <form id="loginForm">'
         + '    <input placeholder="nome" name="name">'
         + '    <input placeholder="senha" type="password" name="password">'
         + '    <button onclick="login(event)">OK</button>'
         + '  </form>'
         + '</div>';

document.write(form);

function login(event) {
    event.preventDefault();
    event.returnValue = false;
    var form = document.getElementById('loginForm');
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            location.reload();
        }
    };
    xhr.open('POST', '/_session', true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send('name=' + form.name.value + '&password=' + form.password.value);
};
var form=''
+ '  '
+ '    '
+ '    '
+“好的”
+ '  '
+ '';
文件。书写(表格);
函数登录(事件){
event.preventDefault();
event.returnValue=false;
var form=document.getElementById('loginForm');
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(xhr.readyState==4){
location.reload();
}
};
xhr.open('POST','/_session',true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
send('name='+form.name.value+'&password='+form.password.value);
};
我不知道其他浏览器,但在Chrome中,创建一个书签并使用此字符串作为URL:


javascript:var-form='OK';文件。书写(表格);函数login(event){event.preventDefault();event.returnValue=false;var form=document.getElementById('loginForm');var xhr=new-XMLHttpRequest();xhr.onreadystatechange=function(){if(xhr.readyState==4){location.reload();}};xhr.open('POST','/\u session true);xhr.setRequestHeader(“内容类型”,“application/x-www-form-urlencoded”);xhr.send('name='+form.name.value+'&password='+form.password.value);}

作为参考,您可以在和中找到详细信息。如果您有改进它的好主意,请提供帮助。否则,Jason的解决方案是您现在能做的最好的解决方案。@JasonSmith我知道这是一个非常古老的思路,但我基本上得出了与您今天早些时候在这里所做的相同的结论,当时我正在思考如何改进有人会这样做。我只是好奇你是否知道这是否已经得到了改进/更新,我可能去哪里了解更多?