DotNetNuke and Custom Permissions

While creating my own  DotNetNuke 5 modules I found myself adding all kinds of custom permission in the code, but then the admin users didn’t have any access to change them.  I found myself having to update the code every time they wanted to add, or remove, a role from a particular permission.

There’s a better way.  In DotNetNuke 5 you can piggyback on the existing permissions functionality.

Let’s say we’re creating a module called Assets and we need a custom permission that allows particular users and roles to assign the asset to other users.  The standard “View Module” and “Edit Module” won’t get us there.

1.First create your customer module and include an App_Code folder.

2. The first thing we need to do is create a class called “Module Security” and place it in the App_Code folder of our module.  The code for the module looks like this (see notes below the code for explanation):

Imports Microsoft.VisualBasic
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Security.Permissions

Namespace MyModules.Modules.Asset
    Public Class ModuleSecurity
        'Constants 
        Public Const PERMISSIONCODE As String = "MYMOD_ASSET"         
        Public Const PN_ASSIGN As String = "ASSET_ASSIGN"        

        'private variables 
        Private _ASSIGN As Boolean = False

        Private _moduleInfo As ModuleInfo

        Sub New(ByVal moduleInfo As ModuleInfo)
            ' TODO: Complete member initialization 
            _moduleInfo = moduleInfo
            ModuleSecurity(moduleInfo)
        End Sub

        Public Sub ModuleSecurity(ByVal moduleInfo As ModuleInfo)
            Dim permCollection As ModulePermissionCollection = moduleInfo.ModulePermissions
            _ASSIGN = ModulePermissionController.HasModulePermission(permCollection, PN_ASSIGN)
        End Sub

        Public ReadOnly Property ASSIGN As Boolean
            Get
                Return _ASSIGN
            End Get
        End Property

    End Class
End Namespace

PermissionCode: This is the Group label for the permissions. All permissions for the module should have the same PermissionCode

PN_Assign: The nomenclature here is “Permission Name Assign”.  I use this for each permission.  Another permission might be PN_Retire – to retire an asset.

2. In your DotNetNuke database use this format and run the following query for each permission you create

Insert into Permission(PermissionCode, ModuleDefID, PermissionKey, PermissionName, ViewOrder, CreatedByUserID, CreatedOnDate, LastModifiedByUserID, LastModifiedOnDate)
Select '{PermissionCode}', ModuleDefID, '{PermissionKey}', '{PermissionName}', 10000, 1, GETDATE(), 1, GETDATE()  
from ModuleDefinitions Where FriendlyName = '{Asset}'

For our example the query would like this:

Insert into Permission(PermissionCode, ModuleDefID, PermissionKey, PermissionName, ViewOrder, CreatedByUserID, CreatedOnDate, LastModifiedByUserID, LastModifiedOnDate)
Select 'MYMOD_ASSET', ModuleDefID, 'ASSET_ASSIGN', 'Assign Asset', 10000, 1, GETDATE(), 1, GETDATE()  
from ModuleDefinitions Where FriendlyName = 'Asset'

The ViewOrder should start at 10000 because DotNetNuke reserves 0-9999 for DotNetNuke permissions.

3. To use the permission in your module put in some code like this:

'At Module level
Private msSecurity As ModuleSecurity
Private msAssign As Boolean = False

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    msSecurity = New ModuleSecurity(Me.ModuleConfiguration)
    msAssign = Convert.ToBoolean(msSecurity.ASSIGN)
    
    'on our page we have an ASP:Button called btnAssign            
    if msAssign Then btnAssign.Visible = False
End Sub

4. The last piece is set up the DotNetNuke module manifest to automatically insert the permissions into the database when the module is installed  The manifest file should look like this:

<moduleDefinitions>
    <moduleDefinition>
        <moduleControls>
             --module definitions deleted for brevity----
        </moduleControls>
        <permissions>
            <permission code="ATPI_ASSET" key="ASSET_ASSIGN" name="Assign Asset" />
        </permissions>
    </moduleDefinition>
</moduleDefinitions>

And that should do it.  Now you’re permissions should show up in the Settings of your module with the DotNetNuke permissions, they should be accessible in your module, and they should automatically populate when the module is installed using the DotNetNuke Extension installer.

Leave a Reply

Your email address will not be published. Required fields are marked *