Apex - 安全性

2019-10-26 16:26 更新

Apex安全性是指對(duì)運(yùn)行代碼應(yīng)用安全設(shè)置和實(shí)施共享規(guī)則的過(guò)程。 Apex類(lèi)具有可以通過(guò)兩個(gè)關(guān)鍵字控制的安全設(shè)置。


數(shù)據(jù)安全性和共享規(guī)則

Apex通常在系統(tǒng)上下文中運(yùn)行;即當(dāng)前用戶(hù)的權(quán)限。在代碼執(zhí)行期間不考慮字段級(jí)安全性和共享規(guī)則。只有匿名塊代碼以執(zhí)行代碼的用戶(hù)權(quán)限執(zhí)行。

我們的Apex代碼不應(yīng)該將敏感數(shù)據(jù)暴露給通過(guò)安全和共享設(shè)置隱藏的用戶(hù)。因此,Apex安全和實(shí)施共享規(guī)則是最重要的。


有共享關(guān)鍵字

如果您使用此關(guān)鍵字,則Apex代碼會(huì)將當(dāng)前用戶(hù)的共享設(shè)置強(qiáng)制為Apex代碼。這不強(qiáng)制配置文件權(quán)限,只有數(shù)據(jù)級(jí)別共享設(shè)置。

讓我們舉一個(gè)例子,我們的用戶(hù)可以訪(fǎng)問(wèn)5個(gè)記錄,但總記錄數(shù)為10.因此,當(dāng)Apex類(lèi)將使用“共享”關(guān)鍵字聲明時(shí),它將只返回5個(gè)用戶(hù)有權(quán)訪(fǎng)問(wèn)的記錄。


例如:

首先,確保您已在Customer對(duì)象中創(chuàng)建了至少10條記錄,其中“5名記錄的名稱(chēng)”為“ABC客戶(hù)”,并將5條記錄保留為“XYZ客戶(hù)”。然后創(chuàng)建一個(gè)共享規(guī)則,與所有用戶(hù)共享“ABC客戶(hù)”。此外,請(qǐng)確保您已將“客戶(hù)”對(duì)象的OWD設(shè)置為“私有”。


將以下代碼粘貼到開(kāi)發(fā)者控制臺(tái)中的Anonymous塊。

//Class With Sharing
public with sharing class MyClassWithSharing {
//Query To fetch 10 records
List<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 class
MyClassWithSharing obj = new MyClassWithSharing();
Integer ListSize = obj.executeQuery();

無(wú)共享關(guān)鍵字

顧名思義,使用此關(guān)鍵字聲明的類(lèi)在系統(tǒng)模式下執(zhí)行,即不考慮用戶(hù)對(duì)記錄的訪(fǎng)問(wèn)權(quán)限,查詢(xún)將獲取所有記錄。

//Class Without Sharing
public 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.

設(shè)置Apex類(lèi)的安全性

您可以為特定配置文件啟用或禁用Apex類(lèi)。 下面是同樣的步驟。 您可以確定哪個(gè)配置文件應(yīng)該具有訪(fǎng)問(wèn)哪個(gè)類(lèi)。


從類(lèi)列表頁(yè)面設(shè)置Apex類(lèi)安全:


步驟1.從安裝程序,單擊開(kāi)發(fā)- >Apex類(lèi)。


類(lèi)列表


第2步:在要限制的類(lèi)的名稱(chēng)旁邊,單擊“安全”.。


apex classes


步驟3.從“可用配置文件”列表中選擇要啟用的配置文件,然后單擊“添加”,或從“已啟用的配置文件”列表中選擇要禁用的配置文件,然后單擊刪除。


配置文件


第4步:單擊保存。


從類(lèi)詳細(xì)信息頁(yè)面設(shè)置Apex類(lèi)安全:


步驟1.從安裝程序,點(diǎn)擊開(kāi)發(fā)- >Apex類(lèi)。


設(shè)置Apex類(lèi)安全


第2步:單擊要限制的類(lèi)的名稱(chēng)。 我們點(diǎn)擊了CustomerOperationClass。


CustomerOperationClass


步驟3單擊安全。


安全

第4步:從“可用配置文件”列表中選擇要啟用的配置文件,然后單擊“添加”,或從“已啟用的配置文件”列表中選擇要禁用的配置文件,然后單擊刪除。


刪除配置文件


步驟5:點(diǎn)擊保存。


從權(quán)限集設(shè)置Apex安全:


第1步設(shè)置,單擊管理用戶(hù)- >權(quán)限集。


管理用戶(hù)


第2步:選擇權(quán)限集。

權(quán)限集


步驟3:單擊Apex類(lèi)訪(fǎng)問(wèn)。


Apex類(lèi)訪(fǎng)問(wèn)


步驟4.單擊編輯。


編輯


第5步:從“可用Apex類(lèi)”列表中選擇要啟用的Apex類(lèi),然后單擊“添加”,或從“已啟用的Apex類(lèi)”列表中選擇要禁用的Apex類(lèi),然后單擊“刪除”。


禁用Apex類(lèi)


第6步:?jiǎn)螕舯4姘粹o。
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)