当前位置:K88软件开发文章中心编程语言非主流编程语言Apex → 文章内容

Apex - 安全性

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:30:55

由 kaikai0220 创建,youj 最后一次修改 2016-12-12 Apex安全性是指对运行代码应用安全设置和实施共享规则的过程。 Apex类具有可以通过两个关键字控制的安全设置。 数据安全性和共享规则Apex通常在系统上下文中运行;即当前用户的权限。在代码执行期间不考虑字段级安全性和共享规则。只有匿名块代码以执行代码的用户权限执行。我们的Apex代码不应该将敏感数据暴露给通过安全和共享设置隐藏的用户。因此,Apex安全和实施共享规则是最重要的。 有共享关键字如果您使用此关键字,则Apex代码会将当前用户的共享设置强制为Apex代码。这不强制配置文件权限,只有数据级别共享设置。让我们举一个例子,我们的用户可以访问5个记录,但总记录数为10.因此,当Apex类将使用“共享”关键字声明时,它将只返回5个用户有权访问的记录。 例如: 首先,确保您已在Customer对象中创建了至少10条记录,其中“5名记录的名称”为“ABC客户”,并将5条记录保留为“XYZ客户”。然后创建一个共享规则,与所有用户共享“ABC客户”。此外,请确保您已将“客户”对象的OWD设置为“私有”。 将以下代码粘贴到开发者控制台中的Anonymous块。//Class With Sharingpublic with sharing class MyClassWithSharing {//Query To fetch 10 recordsList<apex_customer__c> 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 classMyClassWithSharing obj = new MyClassWithSharing();Integer ListSize = obj.executeQuery();无共享关键字顾名思义,使用此关键字声明的类在系统模式下执行,即不考虑用户对记录的访问权限,查询将获取所有记录。//Class Without Sharingpublic without sharing class MyClassWithoutSharing {List<apex_customer__c> 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步:在要限制的类的名称旁边,单击“安全”.。 步骤3.从“可用配置文件”列表中选择要启用的配置文件,然后单击“添加”,或从“已启用的配置文件”列表中选择要禁用的配置文件,然后单击删除。 第4步:单击保存。 从类详细信息页面设置Apex类安全: 步骤1.从安装程序,点击开发- >Apex类。 第2步:单击要限制的类的名称。 我们点击了CustomerOperationClass。 步骤3单击安全。 第4步:从“可用配置文件”列表中选择要启用的配置文件,然后单击“添加”,或从“已启用的配置文件”列表中选择要禁用的配置文件,然后单击删除。 步骤5:点击保存。 从权限集设置Apex安全: 第1步设置,单击管理用户- >权限集。 第2步:选择权限集。 步骤3:单击Apex类访问。 步骤4.单击编辑。 第5步:从“可用Apex类”列表中选择要启用的Apex类,然后单击“添加”,或从“已启用的Apex类”列表中选择要禁用的Apex类,然后单击“删除”。 第6步:单击保存按钮。

Apex - 安全性