Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Last active August 29, 2015 14:13
Show Gist options
  • Save tyoshikawa1106/187be2ef466914d7fe7f to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/187be2ef466914d7fe7f to your computer and use it in GitHub Desktop.
Apex Security Check Sample
<apex:page controller="AccountRegisterController" showHeader="true" sidebar="false" tabStyle="Account" id="page">
<div id="vf-page">
<apex:sectionHeader title="{!$ObjectType.Account.Label}" subtitle="Account Register" />
<apex:form id="form">
<apex:pageBlock title="Account Register" mode="edit" id="block">
<apex:pagemessages id="msg"/>
<apex:pageBlockButtons location="bottom" id="buttons">
<apex:commandButton value=" Save " title=" Save " action="{!doSave}" reRender="form" id="saveBtn" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Account Information" id="section">
<apex:inputField value="{!account.Name}" />
<apex:inputField value="{!account.AccountNumber}" />
<apex:inputField value="{!account.Phone}" />
<apex:inputField value="{!account.Fax}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</div>
</apex:page>
public with sharing class AccountRegisterController {
private AccountRegisterHelper helper = new AccountRegisterHelper();
public Account account {get; set;}
public AccountRegisterController() {
// Default
this.account = new Account();
//this.account.AccountNumber = 'A-0001';
// Check Accessible
if (!this.helper.isAccessible()) return;
}
public PageReference doSave() {
// Check Security
if (!this.helper.isCreateable()) return null;
if (!this.helper.isUpdateable()) return null;
if (!this.helper.isDeletable()) return null;
// Account Upsert
upsert this.account;
return new PageReference('/' + this.account.Id);
}
}
public with sharing class AccountRegisterHelper {
public AccountRegisterHelper() {
}
/**
* isAccessible
*/
public Boolean isAccessible() {
Boolean result = true;
if (!Schema.sObjectType.Account.Fields.Name.isAccessible()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Name.Label + ': Not Accessible');
result = false;
}
if (!Schema.sObjectType.Account.Fields.AccountNumber.isAccessible()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.AccountNumber.Label + ': Not Accessible');
result = false;
}
if (!Schema.sObjectType.Account.Fields.Phone.isAccessible()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Phone.Label + ': Not Accessible');
result = false;
}
if (!Schema.sObjectType.Account.Fields.Fax.isAccessible()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Fax.Label + ': Not Accessible');
result = false;
}
return result;
}
/**
* isCreateable
*/
public Boolean isCreateable() {
Boolean result = true;
if (!Schema.sObjectType.Account.Fields.Name.isCreateable()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Name.Label + ': Not Creatable');
result = false;
}
if (!Schema.sObjectType.Account.Fields.AccountNumber.isCreateable()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.AccountNumber.Label + ': Not Creatable');
result = false;
}
if (!Schema.sObjectType.Account.Fields.Phone.isCreateable()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Phone.Label + ': Not Creatable');
result = false;
}
if (!Schema.sObjectType.Account.Fields.Fax.isCreateable()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Fax.Label + ': Not Creatable');
result = false;
}
return result;
}
/**
* isUpdateable
*/
public Boolean isUpdateable() {
Boolean result = true;
if (!Schema.sObjectType.Account.Fields.Name.isUpdateable()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Name.Label + ': Not Updatable');
result = false;
}
if (!Schema.sObjectType.Account.Fields.AccountNumber.isUpdateable()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.AccountNumber.Label + ': Not Updatable');
result = false;
}
if (!Schema.sObjectType.Account.Fields.Phone.isUpdateable()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Phone.Label + ': Not Updatable');
result = false;
}
if (!Schema.sObjectType.Account.Fields.Fax.isUpdateable()) {
CommonUtil.msgError('[' + Schema.sObjectType.Account.Label + '] ' + Schema.sObjectType.Account.Fields.Fax.Label + ': Not Updatable');
result = false;
}
return result;
}
/**
* isDeletable
*/
public Boolean isDeletable() {
Boolean result = true;
if (!Schema.sObjectType.Account.isDeletable()) {
CommonUtil.msgError(Schema.sObjectType.Account.Label + ': Not Deletable');
result = false;
}
return result;
}
}
public with sharing class CommonUtil {
public static void msgError(String prmMsg) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, prmMsg));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment