User Tools

Site Tools


apex:crud_and_fls_enforcement

CRUD and FLS enforcement

public with sharing class checkCRUDandFLS {
 
  public checkCRUDandFLS() {}
 
  public Set<String> checkFields(Schema.DescribeSObjectResult dor,List<String> fieldNames,String lvl) {
    Set<String> toReturn = new Set<String>();
    if(dor==null) {
      toReturn.add('Schema.DescribeSObjectResult = null');
    } else {
      if((lvl!='C')&&(lvl!='R')&&(lvl!='U')&&(lvl!='D')) {
        toReturn.add(System.Label.VFPL_Unknown_CRUD_FLS_check.replace('|check|',lvl));
      } else {
        toReturn.addAll(checksObject(dor,lvl));
        String objName = dor.getName();
        for(String name: fieldNames) {
          if(dor.fields.getMap().get(name)!=null) {
            String fldName = dor.fields.getMap().get(name).getDescribe().getName();
            if(lvl=='C') {
              if(!dor.fields.getMap().get(name).getDescribe().isCreateable()) {
                toReturn.add(System.Label.VFPL_Field_isn_t_Createable.replace('|fldName|',fldName).replace('|objName|',objName));
              }
            } else if(lvl=='R') {
              if(!dor.fields.getMap().get(name).getDescribe().isAccessible()) {
                toReturn.add(System.Label.VFPL_Field_isn_t_Accessible.replace('|fldName|',fldName).replace('|objName|',objName));
              }
            } else if(lvl=='U') {
              if(!dor.fields.getMap().get(name).getDescribe().isUpdateable()) {
                toReturn.add(System.Label.VFPL_Field_isn_t_Updateable.replace('|fldName|',fldName).replace('|objName|',objName));
              }
            }
          } else {
            toReturn.add(System.Label.VFPL_Unknown_field_name_on_object.replace('|fldName|',name).replace('|objName|',objName));
          }
        }
      }
    }
    return toReturn;
  }
 
  public Set<String> checksObject(Schema.DescribeSObjectResult dor, String lvl) {
    Set<String> toReturn = new Set<String>();
    String objName = dor.getName();
    if(lvl=='C') {
      if(!dor.isCreateable()) {
        toReturn.add(System.Label.VFPL_Object_isn_t_Createable.replace('|objName|',objName));
      }
    } else if(lvl=='R') {
      if(!dor.isAccessible()) {
        toReturn.add(System.Label.VFPL_Object_isn_t_Accessible.replace('|objName|',objName));
      }
    } else if(lvl=='U') {
      if(!dor.isUpdateable()) {
        toReturn.add(System.Label.VFPL_Object_isn_t_Updateable.replace('|objName|',objName));
      }
    } else if(lvl=='D') {
      if(!dor.isDeletable()) {
        toReturn.add(System.Label.VFPL_Object_isn_t_Deletable.replace('|objName|',objName));
      }
    }
    return toReturn;
  }
}
apex/crud_and_fls_enforcement.txt · Last modified: 2015/10/29 23:50 by vitaly