Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if user has module edit permission when page is not in edit mode

I am developing a module for DNN 7.1+ and I need to display / hide a link in the module based on if the user has edit permission for that module. I want this to happen regardless of whether or not the page is in edit mode.

Currently I have the following code in the view.ascx of the custom module page load event:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Try
        ''other code goes here.......
        If IsEditable = True Then
            AdminEdit.Visible = True
        Else
            AdminEdit.Visible = False
        End If
    Catch exc As Exception
        Exceptions.ProcessModuleLoadException(Me, exc)
    End Try

End Sub

AdminEdit is the ID of the element I want to hide. This code works when the page is in edit mode, but I want this to always be visible, regardless of edit mode, if the user has edit permission for that module.

Right now the IsEditbale returns false if page is not in edit mode.

QUESTION: How can I check the user permission without the page being in edit mode?

EDIT: I would also be happy with checking against the user's edit permission for the page containing the module

SOLUTION:

Here is what I ended up with thanks to bdukes:

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try

            ''display template selector if user has edit rights over module
            If DotNetNuke.Security.Permissions.ModulePermissionController.CanEditModuleContent(Me.ModuleConfiguration) Then
                AdminEdit.Visible = True
            Else
                AdminEdit.Visible = False
            End If
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try

    End Sub
like image 623
J King Avatar asked Feb 14 '23 10:02

J King


1 Answers

All that IsEditable does is tell you if you're in edit mode. To check permissions, use DotNetNuke.Security.Permissions.ModulePermissionController.CanEditModuleContent(modInfo).

Checking the permissions of users other than the current user is quite a bit messier and more fragile. At a basic level, you can call PortalSecurity.IsInRoles and follow the example at PermissionProvider to pass in modInfo.ModulePermissions.ToString("EDIT"). However, "EDIT" here is a value defined in the permission provider, and Evoq uses a different permission provider (in order to provide more granular permissions), so it's possible that different environment may have different permissions you want to check.

like image 133
bdukes Avatar answered Apr 28 '23 12:04

bdukes