首页 > 解决方案 > 如何检查用户是否能够在 marklogic 数据库中更新或插入文档?

问题描述

如何检查用户是否能够在 marklogic 数据库中更新或插入任何文档?

例如,在marklogic数据库中,有4个用户,有的有更新权限,有的有读文档的权限。

try{
    let $uri := abc.xml
    let $doc : <a/>
    if (condition)
    then check whether the current user is able to update or insert the doc in marklogic or not , if it is not then throw fn:error()
    else 
    xdmp:document-insert($uri,$doc) (:it will throw error, when user have no permission to insert the doc:)
    }
catch($e)
{$e}

标签: securityauthenticationpermissionsmarklogicroles

解决方案


用户插入和更新文档所需的权限将取决于用户的显式角色和权限,以及默认权限和对文档设置的任何显式权限。

https://docs.marklogic.com/guide/admin/security#chapter

在此处输入图像描述

https://docs.marklogic.com/xdmp:document-insert

所需权限 如果插入新文档,则还需要unprotected-uri权限(仅当 URI 不受保护时)、any-uri权限或适当的 URI 权限。如果向文档添加不受保护的集合,unprotected-collections则需要该权限;如果添加受保护的集合,用户必须具有更新集合的权限或any-collection特权。

如果您要更新文档,则您必须具有为该文档指定的必要权限(可以包括默认权限)。

用于xdmp:document-get-permissions返回哪些角色对该特定 URI 具有哪些权限,然后将其与附加到感兴趣的用户的角色相交,您将知道用户是否可以访问或更新文档。

因此,要检查用户是否具有插入或更新 URI 的能力,您需要获取该用户的角色,然后查看默认权限或文档权限中的权限是否具有该角色以及插入或更新能力:

xquery version "1.0-ml";
import module namespace sec="http://marklogic.com/xdmp/security" at "/MarkLogic/security.xqy";
let $name := "user-foo"
let $uri := "/bar.xml"
let $user-roles := xdmp:invoke-function(
  function(){ sec:user-get-roles($name) }, 
  <options xmlns="xdmp:eval">
    <database>{xdmp:security-database()}</database>
  </options>)
let $permissions := (xdmp:default-permissions(), xdmp:document-get-permissions($uri))
return
 exists($permissions[sec:capability=('insert', 'update') and sec:role-id/xdmp:role-name(.) = $user-roles])

推荐阅读