vlocode-project - v1.40.0-beta-4
    Preparing search index...

    Class SalesforceUserPermissions

    Represents a Salesforce user permissions model, providing methods to manage and manipulate metadata for profiles and permission sets. This class allows for loading, merging, updating, and saving metadata related to user permissions, such as field permissions, object permissions, Apex class access, and record type visibility.

    • The class supports XML parsing and serialization for Salesforce metadata.
    • Changes to the metadata are tracked, and only updated fields are saved back to Salesforce.
    • Provides utility methods for checking, adding, updating, and removing permissions for various Salesforce components like fields, pages, classes, and record types.
    const profile = new SalesforceUserPermissions('Profile', 'CustomProfile');
    profile.addField('Account.Name', 'editable');
    profile.setRecordTypeVisibility('Account.CustomRecordType', true);
    await profile.save(connection);

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    "#hasChanges": boolean = false
    arrayProperties: string[] = ...
    developerName: string
    metadata: Required<UserPermissionMetadata>
    metadataUpdates: Partial<UserPermissionMetadata> & { fullName: string }
    profileSorter: UserPermissionSortConfig = ...
    type: "PermissionSet" | "Profile"

    Accessors

    Methods

    • Adds or updates the access permissions for an Apex class. If the class does not exist in the current metadata, it will be added with the specified enabled state. If the class already exists, its enabled state will be updated if it differs from the provided value.

      Parameters

      • name: string

        The name of the Apex class to add or update.

      • enabled: boolean

        A boolean indicating whether access to the class is enabled.

      Returns void

    • Adds a field permission to the profile model. If the field already exists, its permissions are updated.

      Parameters

      • field: string

        The name of the field to add or update.

      • access: SalesforceFieldPermission

        The access level for the field, which can be 'editable' or 'readable'. - 'editable': Grants both read and write access to the field. - 'readable': Grants read-only access to the field.

      Returns void

      If the field already exists in the profile model, its permissions are updated based on the provided access level. If the field does not exist, it is added to the profile model with the specified permissions.

      profile.addField('Account.Name', 'editable'); // Adds or updates the field with editable access.
      profile.addField('Account.Phone', 'readable'); // Adds or updates the field with readable access.
    • Adds or updates the access permissions for an Apex page. If the page already exists in the permissions list, its enabled status is updated. Otherwise, the page is added to the metadata with the specified enabled status.

      Parameters

      • name: string

        The name of the Apex page to add or update.

      • enabled: boolean

        A boolean indicating whether the page access should be enabled.

      Returns void

    • Determines whether a user has access to a specific Apex class.

      Parameters

      • name: string

        The name of the Apex class to check access for.

      Returns undefined | boolean

      true if the user has access to the specified class, false if access is disabled, or undefined if the class is not found.

    • Retrieves the field access permission for a specified field name.

      Parameters

      • fieldName: string

        The name of the field to check access permissions for.

      Returns undefined | SalesforceFieldPermission

      The field access permission as a SalesforceFieldPermission value: - SalesforceFieldPermission.editable if the field is editable. - SalesforceFieldPermission.readable if the field is readable but not editable. - SalesforceFieldPermission.none if the field exists but has no access permissions. - undefined if the field does not exist.

    • Determines whether the user has access to a specific Apex class by name.

      Parameters

      • name: string

        The name of the Apex class to check for access.

      Returns boolean

      true if the user has access to the specified Apex class; otherwise, false.

    • Checks if the profile contains a specific field.

      Parameters

      • ...field: string[]

        One or more strings representing the field name(s) to check. If multiple strings are provided, they are joined with a dot (.) to form a composite field name.

      Returns boolean

      true if the profile contains the specified field, otherwise false.

    • Checks if the user has access to a specific Apex page by its name.

      Parameters

      • name: string

        The name of the Apex page to check.

      Returns boolean

      true if the user has access to the specified Apex page; otherwise, false.

    • Loads and parses metadata XML content, merging the parsed data into the current instance.

      Parameters

      • metadataXml: string | Buffer<ArrayBufferLike>

        The metadata XML content to load, provided as a string or Buffer.

      Returns this

      The current instance with the merged data.

      The method uses custom parsing logic to handle specific data types:

      • Boolean values are parsed from "true" or "false" strings (case-insensitive).
      • Integer values are parsed from numeric strings.
      • Floating-point values are parsed from decimal strings.
      • Other values are returned as-is.

      Array properties are determined based on the arrayProperties configuration.

    • Merges the provided partial user permission metadata into the current metadata object. This method normalizes keys to ensure case-insensitivity and handles merging of array values. If the key exists in the current metadata and is an array, the values are appended. Otherwise, the value is directly assigned to the metadata. Unmapped properties are preserved and added to the metadata.

      Parameters

      Returns void

    • Removes an item from the specified metadata and metadataUpdates arrays based on the provided key and name.

      Type Parameters

      • K extends
            | "description"
            | "objectPermissions"
            | "applicationVisibilities"
            | "classAccesses"
            | "customMetadataTypeAccesses"
            | "customPermissions"
            | "customSettingAccesses"
            | "externalDataSourceAccesses"
            | "fieldPermissions"
            | "flowAccesses"
            | "pageAccesses"
            | "recordTypeVisibilities"
            | "userPermissions"
            | "fullName"

      Parameters

      • key: K

        The key representing the property in the metadata object. This key must correspond to an array property.

      • name: string

        The name or identifier of the item to be removed. This is matched against the field specified by PermissionNameFields[key].

      Returns void

      Throws an error if the provided key does not correspond to an array property.

    • Saves the current metadata changes to Salesforce using the provided connection. If there are no changes, the method returns immediately without performing any operation.

      Parameters

      • connection: SalesforceConnection

        The Salesforce connection instance used to perform the metadata update.

      Returns Promise<void>

      If the save operation fails, an error is thrown with details about the failure.

    • Sets the visibility of a specific record type for the current profile. If the record type visibility already exists, it updates the visibility. Otherwise, it adds a new record type visibility entry to the metadata.

      Parameters

      • recordTypeName: string | { name: string; objectType: string }

        The name of the record type to set visibility for.

      • visible: boolean = true

        The visibility status to set for the record type. Defaults to true.

      Returns void

    • Update entries on an array property of the profile object. If the new value is different than the current value, the change will be tracked.

      Type Parameters

      • K extends
            | "description"
            | "objectPermissions"
            | "applicationVisibilities"
            | "classAccesses"
            | "customMetadataTypeAccesses"
            | "customPermissions"
            | "customSettingAccesses"
            | "externalDataSourceAccesses"
            | "fieldPermissions"
            | "flowAccesses"
            | "pageAccesses"
            | "recordTypeVisibilities"
            | "userPermissions"
            | "fullName"

      Parameters

      Returns void