×
Apex 编程教程Apex 环境Apex 示例Apex 数据类型Apex 变量Apex 字符串Apex 数组Apex 常量Apex 决策Apex 循环Apex 集合Apex 类Apex 类方法Apex 对象Apex 接口Apex DMLApex 数据库方法Apex SOSLApex SOQLApex 安全性Apex 调用Apex 触发器Apex 触发设计模式Governer Limits调节器限制Apex 批量处理Apex 调试Apex 测试Apex 部署Apex 字符串Apex 数组

Apex 安全性


Apex安全性是指对运行代码应用安全设置和实施共享规则的过程。 Apex类具有可以通过两个关键字控制的安全设置。


数据安全性和共享规则

Apex通常在系统上下文中运行;即当前用户的权限。在代码执行期间不考虑字段级安全性和共享规则。只有匿名块代码以执行代码的用户权限执行。

我们的Apex代码不应该将敏感数据暴露给通过安全和共享设置隐藏的用户。因此,Apex安全和实施共享规则是最重要的。


有共享关键字

如果您使用此关键字,则Apex代码会将当前用户的共享设置强制为Apex代码。这不强制配置文件权限,只有数据级别共享设置。

让我们举一个例子,我们的用户可以访问5个记录,但总记录数为10.因此,当Apex类将使用“共享”关键字声明时,它将只返回5个用户有权访问的记录。


例如:

首先,确保您已在Customer对象中创建了至少10条记录,其中“5名记录的名称”为“ABC客户”,并将5条记录保留为“XYZ客户”。然后创建一个共享规则,与所有用户共享“ABC客户”。此外,请确保您已将“客户”对象的OWD设置为“私有”。


将以下代码粘贴到开发者控制台中的Anonymous块。

//Class With Sharing
public with sharing class MyClassWithSharing {
//Query To fetch 10 records
List CustomerList = [SELECT id, Name FROM APEX_Customer__c LIMIT 10];
	
public Integer executeQuery () {
    System.debug('List will have only 5 records and the actual records are '+CustomerList.size()+' as user has access to'+CustomerList);
    Integer ListSize = CustomerList.size();
    return ListSize;
}
}

//Save the above class and then execute as below
//Execute class using the object of class
MyClassWithSharing obj = new MyClassWithSharing();
Integer ListSize = obj.executeQuery();

无共享关键字

顾名思义,使用此关键字声明的类在系统模式下执行,即不考虑用户对记录的访问权限,查询将获取所有记录。

//Class Without Sharing
public without sharing class MyClassWithoutSharing {
List CustomerList = [SELECT id, Name FROM APEX_Customer__c LIMIT 10];//Query To fetch 10 records, this will return all the records
    
public Integer executeQuery () {
    System.debug('List will have only 5 records and the actula records are '+CustomerList.size()+' as user has access to'+CustomerList);
    Integer ListSize = CustomerList.size();
    return ListSize;
}
}
//Output will be 10 records.

设置Apex类的安全性

您可以为特定配置文件启用或禁用Apex类。 下面是同样的步骤。 您可以确定哪个配置文件应该具有访问哪个类。


从类列表页面设置Apex类安全:


步骤1.从安装程序,单击开发- >Apex类。


类列表


第2步:在要限制的类的名称旁边,单击“安全”.。


apex classes


步骤3.从“可用配置文件”列表中选择要启用的配置文件,然后单击“添加”,或从“已启用的配置文件”列表中选择要禁用的配置文件,然后单击删除。


配置文件


第4步:单击保存。


从类详细信息页面设置Apex类安全:


步骤1.从安装程序,点击开发- >Apex类。


设置Apex类安全


第2步:单击要限制的类的名称。 我们点击了CustomerOperationClass。


CustomerOperationClass


步骤3单击安全。


安全

第4步:从“可用配置文件”列表中选择要启用的配置文件,然后单击“添加”,或从“已启用的配置文件”列表中选择要禁用的配置文件,然后单击删除。


删除配置文件


步骤5:点击保存。


从权限集设置Apex安全:


第1步设置,单击管理用户- >权限集。


管理用户


第2步:选择权限集。

权限集


步骤3:单击Apex类访问。


Apex类访问


步骤4.单击编辑。


编辑


第5步:从“可用Apex类”列表中选择要启用的Apex类,然后单击“添加”,或从“已启用的Apex类”列表中选择要禁用的Apex类,然后单击“删除”。


禁用Apex类


第6步:单击保存按钮。

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)