Hi,
I am adding new users from an uploaded excel spreadsheet and adding a Specific Role [in a Role Group] to them.
I've written code [below ] to do this successfully, but there are a couple of outstanding [minor] issues.
1. Because there isn't a "GetRoleGroupByName" method on the DotNetNuke.Security.Roles.RoleController,
I have to iterate through the returned arraylist from "GetRoleGroups".
Can "GetRoleGroupByName" be added to the class??
2. If the RoleGroup Doesn't exist, I create it [successfully]. Problem is, the following code doesn't appear to recognise that it's there.
A check On the Admin>Security Roles Show that the group And role was created And Subsequent calls To the code are good With the user being created With the roles in the group As expected.
I suspect this is a caching issue. Any thoughts?
code:
Rec_UserInfo = DotNetNuke.Entities.Users.UserController.GetUserByName(Me.PortalId, RecPMQ_Users.User_Logon)
If Rec_UserInfo Is Nothing Then
'Now, create a new DNN User for this IFA
Rec_UserInfo = New DotNetNuke.Entities.Users.UserInfo
Rec_UserInfo.FirstName = "Fred"
Rec_UserInfo.LastName = "Bloggs"
Rec_UserInfo.Username = "logob Phrase"
'Membership details
Rec_UserInfo.Membership.Password = "Password"
Rec_UserInfo.DisplayName = "Fred Bloggs"
Rec_UserInfo.Email = "FB @ Foo.com"
Rec_UserInfo.Membership.Approved = True
Rec_UserInfo.PortalID = Me.PortalId
Rec_UserInfo.Profile.FirstName = Rec_UserInfo.FirstName
Rec_UserInfo.Profile.LastName = Rec_UserInfo.LastName
Rec_UserInfo.Profile.Telephone = RecPMQ_Users.User_MobilePhone
Rec_UserInfo.IsSuperUser = False
Dim createStatus As DotNetNuke.Security.Membership.UserCreateStatus = DotNetNuke.Entities.Users.UserController.CreateUser(Rec_UserInfo)
If createStatus = DotNetNuke.Security.Membership.UserCreateStatus.Success Then
SuccessfulEntries += 1
'Add this user to the Designated Role
Rec_UserInfo = DotNetNuke.Entities.Users.UserController.GetUserByName(Me.PortalId, "Logon Phrase")
If Rec_UserInfo Is Nothing Then
Me.lbl_Stage3Message.Text = "There was a problem setting up the User record in the database - Please call Administration"
Else
Dim oDate As Date = CDate("1/1/2020")
Dim RC As New DotNetNuke.Security.Roles.RoleController
'Check for RoleGroup.
Dim ColRoleGroups As ArrayList
Dim GroupId As Integer = -1
Dim k As Integer
ColRoleGroups = DotNetNuke.Security.Roles.RoleController.GetRoleGroups(PortalId)
For k = 0 To ColRoleGroups.Count - 1
If CType(ColRoleGroups(k), DotNetNuke.Security.Roles.RoleGroupInfo).RoleGroupName = "Project Group" Then
GroupId = CType(ColRoleGroups(k), DotNetNuke.Security.Roles.RoleGroupInfo).RoleGroupID
Exit For
End If
Next
If GroupId = -1 Then
'Create new RoleGroup
Dim oRoleGroup As New DotNetNuke.Security.Roles.RoleGroupInfo
oRoleGroup.Description = "Project Group for Users"
oRoleGroup.PortalID = PortalId
oRoleGroup.RoleGroupName = "Project Group"
GroupId = DotNetNuke.Security.Roles.RoleController.AddRoleGroup(oRoleGroup)
End If
Dim oRoleCheck As DotNetNuke.Security.Roles.RoleInfo
oRoleCheck = RC.GetRoleByName(Me.PortalId, "Manager")
If oRoleCheck Is Nothing Then
'Create the Role.
oRoleCheck = New DotNetNuke.Security.Roles.RoleInfo
oRoleCheck.PortalID = PortalId
oRoleCheck.RoleName = "Manager"
oRoleCheck.Description = "Project Role for the Manager"
oRoleCheck.ServiceFee = 0
oRoleCheck.BillingPeriod = 0
oRoleCheck.BillingFrequency = ""
oRoleCheck.TrialFee = 0
oRoleCheck.TrialPeriod = 0
oRoleCheck.IsPublic = False
oRoleCheck.AutoAssignment = False
oRoleCheck.RoleGroupID = GroupId
Dim role_ID As Integer
role_ID = RC.AddRole(oRoleCheck)
DotNetNuke.Common.Utilities.DataCache.RemoveCache("GetRoles")
End If
Dim oRole As DotNetNuke.Security.Roles.RoleInfo
oRole = RC.GetRoleByName(Me.PortalId, CType(ObjCntroller.PMQ_GetChoiceByValue(PMQ_BindTypes.Roles, CInt(params(3))), PMQ_Choices).PMQ_ChoiceDescription)
If Not oRole Is Nothing Then
RC.AddUserRole(Me.PortalId, Rec_UserInfo.UserID, oRole.RoleID, oDate)
Else
Me.lbl_Stage3Message.Text = "There was a problem adding the Project Role to the User - Please call Administration"
Exit Sub
End If
End If