fredag den 9. september 2011

SuperOffice 7 – Creating a new user

24-09-2011: This can be done more smoothly, please see this post.

God I spend way to much time finding a solution for this

    Function CreateNewUser(mySession As SuperOffice.SoSession, ADLogin As Boolean, sFullName As String, loginName As String, sEmail As String, sPassword As String) As SuperOffice.CRM.Administration.SoUser
Dim soDB As SoDatabase = SoDatabase.GetCurrent()
Dim Person As Person = Person.CreateNew
Person.SetDefaults()
Person.Firstname
= Mid(sFullName, 1, InStr(sFullName, " ") - 1)
Person.Lastname
= Mid(sFullName, InStr(sFullName, " ") + 1)
Person.Contact
= GetPrimaryContact()
Person.Save()

Dim user As SuperOffice.CRM.Administration.SoUser = SuperOffice.CRM.Administration.SoUser.CreateNew(Person.PersonId, License.UserType.InternalAssociate)
user.LogonName
= loginName
'user.SetValidUserName(loginName)
user.LoginRight = True
user.LicenseFieldRight.DemandRight(CRM.Security.EFieldRight.Read)
user.SetPassword(sPassword)
user.RoleIdx
= 1
user.GroupIdx
= 1
user.OtherGroupIds
= New Integer() {2, 3, 4}
If ADLogin Then
Dim ADUser = FindADUSer(sEmail)
Dim SID As New System.Security.Principal.SecurityIdentifier(ADUser.Properties("objectSid").Value, 0)
user.AddCredential(
"ActiveDirectory", SID.Value, sEmail)
End If
user.Save()
Return user
End Function

So we start by creating a Person and then we associate a new user with this person. We set the basic information for the user and if the user is going to be a windows user, add some Windows Credentials by getting the user’s SID from ad.
Here is a quick way to find a user by UPN


    Function FindADUSer(upn As String) As System.DirectoryServices.DirectoryEntry
Try
Dim defaultNamingContext As String
Using rootDSE As New System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
defaultNamingContext
= rootDSE.Properties("defaultNamingContext").Value.ToString()
End Using

Dim entry As New System.DirectoryServices.DirectoryEntry("LDAP://" & defaultNamingContext)
Dim search As New System.DirectoryServices.DirectorySearcher(entry)
search.Filter
= "(userPrincipalName=" & upn & ")"
search.PropertiesToLoad.Add(
"sAMAccountName")
search.PropertiesToLoad.Add(
"userPrincipalName")
search.PropertiesToLoad.Add(
"objectSid")
Dim result As System.DirectoryServices.SearchResultCollection = search.FindAll()
For Each res As System.DirectoryServices.SearchResult In result
Return res.GetDirectoryEntry
Next
Catch ex As Exception
Throw ex
End Try
Throw New Exception("Could not find user in AD")
End Function

Cool, so we have a user that can login and do everything, (though code) but you cant open the SuperOffice client. To do that you need to assign a license to the user.


    Function GetIndexByModuleOwner(SOUser As SuperOffice.CRM.Administration.SoUser, ownername As String) As Integer
'SuperOffice.License.AssociateModuleLicenseOwner
For i As Integer = LBound(SOUser.Licenses) To UBound(SOUser.Licenses)
Dim lic = SOUser.Licenses(i)
If lic.Owner.OwnerName.ToLower = ownername.ToLower Then
Return i
End If
Next
Return 0
End Function

Sub SetUserLicense(User As SuperOffice.CRM.Administration.SoUser, Enabled As Boolean)
Dim licenseModuleOwner As Integer = GetIndexByModuleOwner(User, "Superoffice")
If licenseModuleOwner >= 0 Then
'LicAssocLnk[ModuleOwner (int) ][ModuleLicense (int or string)]
'User.Licenses(licenseModuleOwner)(SuperOffice.License.SoLicenseNames.Web).Assigned = True
User.Licenses(licenseModuleOwner)(SuperOffice.License.SoLicenseNames.User).Assigned = Enabled
User.Licenses(licenseModuleOwner)(SuperOffice.License.SoLicenseNames.Windows).Assigned
= Enabled
User.Save()
End If
End Sub

Sub SetTravelLicense(User As SuperOffice.CRM.Administration.SoUser, Enabled As Boolean, RemoteTravel As Boolean)
Dim licenseModuleOwner As Integer = GetIndexByModuleOwner(User, "Superoffice")
If licenseModuleOwner >= 0 Then
'LicAssocLnk[ModuleOwner (int) ][ModuleLicense (int or string)]
User.Licenses(licenseModuleOwner)(SuperOffice.License.SoLicenseNames.Travel).Assigned = Enabled
User.Licenses(licenseModuleOwner)(SuperOffice.License.SoLicenseNames.RemoteTravel).Assigned
= RemoteTravel
User.Save()
If Enabled Then SetUserLicense(User, Enabled)
End If
End Sub

I decided to split this up a bit. I’m going to be toggling the features a lot so makes more sense in my code.


So now we can just


        SoSession.Authenticate("user", "supersecretpassword")
Dim user As CRM.Administration.SoUser = CreateNewUser(mysession, True, "Allan Zimmermann", "AZ", "alz@wingu.dk", "MegaHemmeligt!")
SetUserLicense(user,
True)
SetTravelLicense(user,
True, True)

1 kommentar:

  1. Lastly, net site} is cell optimized, but there are no obtainable apps for cell units. The minimal withdrawal, the time wanted to course of withdrawals, and whether or not there's a charge will all depend upon the chosen technique. The platform is quite of|is type of} secure, with sturdy encryption, customer support obtainable 24/7 through reside chat and e-mail, and other security measures. Also, its service doesn't have cell apps, nevertheless 카지노 it has an internet site|a internet site} that is totally optimized for cell play.

    SvarSlet