ZH-CN/hasObjectPermissionTo

From Multi Theft Auto: Wiki
Revision as of 07:26, 6 February 2021 by Qwe7769611 (talk | contribs)
Jump to navigation Jump to search

此函数返回给定对象是否有权执行给定操作.

[[{{{image}}}|link=|]] Note: Only certain action names work. This function seems to return nil and output a bad argument error when checking if an object has rights for an action which doesn't start with function., command. or resource. keywords.

脚本经常希望限制特定用户对函数的访问.最简单的方法是检查试图执行某个操作的玩家是否在特定的组(通常是Admin组)中.这样做的主要问题是管理组不能保证存在.它也没有给服务器管理员任何灵活性.他可能希望允许他的“moderators”访问您限制访问的功能,或者他可能希望完全禁用该功能.

这就是正确使用ACL的地方,幸运的是这非常容易。归根结底就是使用这个函数.这个名称有些混乱的函数允许您检查ACL对象(player或source)是否具有特定的ACL权限。在这种情况下,我们只关心玩家们.

所以,首先,为你的“right”想个名字。假设我们想要一个只有某些人才能进入的私人区域,我们会称之为我们的right accessPrivateArea.然后,您只需在代码中添加一个“if”语句:

if hasObjectPermissionTo ( player, "resource.YourResourceName.accessPrivateArea", false ) then
-- 如果他们被允许进来,你想发生什么都可以
else
-- 不管你想发生什么,如果他们不是
end

请注意,我们使用“right”来命名“right”resource.YourResourceName.accessPrivateArea-这只是为了整洁,以便管理员知道权限属于什么资源. 强烈建议你遵守这个惯例. “false”参数指定“defaultPermission”, false表示如果用户没有被允许或不允许的权限(即管理员没有将其添加到配置中),它应该默认为不被允许.

使用此方法的唯一缺点是管理员必须修改其配置。好处是管理员有更多的控制权,您的脚本可以在任何服务器上运行,不管管理员如何配置它.

语法

bool hasObjectPermissionTo ( string / element theObject, string theAction [, bool defaultPermission = true ] )

OOP 语法 什么是OOP?

提示: This function is also a static function underneath the ACL class.
方法: ACL.hasObjectPermissionTo(...)

必填参数

  • theObject: 要测试的对象是否有访问的权限。它可以是客户端元素(即player)、resource或“user.<name>”或“resource.<name>”形式的字符串.
  • theAction: 测试给定对象是否有权访问的操作. 就是说. "function.kickPlayer".

选填参数=

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • defaultPermission: The default permission if none is specified in either of the groups the given object is a member of. If this is left to true, the given object will have permissions to perform the action unless the opposite is explicitly specified in the ACL. If false, the action will be denied by default unless explicitly approved by the Access Control List.

返回值

如果给定对象具有执行给定操作的权限,则返回“true”,否则返回“false”。如果函数因参数错误而失败,则返回“nil”.

示例

如果使用它的用户可以访问kickPlayer函数,那么这个示例将踢出一个玩家.

-- 踢出指令
function onKickCommandHandler ( playerSource, commandName, playerToKick, stringReason )
    -- 呼叫用户是否有权踢球员?违约
    -- 出于安全原因而伪造.我们这样做是为了任何用户都不能使用我们
    -- 踢出玩家.
    if ( hasObjectPermissionTo ( playerSource, "function.kickPlayer", false ) ) then

        -- 我们有权限踢出那个玩家吗? We do this so we can fail
        -- 如果这个资源不能调用这个函数.
        if ( hasObjectPermissionTo ( getThisResource (), "function.kickPlayer", true ) ) then
            -- 踢出他
            kickPlayer ( playerToKick, playerSource, stringReason )
        else
            -- 资源没有任何权限,抱歉
            outputChatBox ( "kick: The admin resource is not able to kick players. Please give this resource access to 'function.kickPlayer' in the ACL to use this function.", playerSource )
        end
    else
        -- 用户没有任何权限
        outputChatBox ( "kick: You don't have permissions to use this command.", playerSource )
    end
end
addCommandHandler ( "kick", onKickCommandHandler )

See Also