« Posts by DevJonny

Friendlier Friendly URL Mapper for Liferay 6

It’s been a while since I written a post, but last Thursday I was working on Liferay portlet which involved using Liferay’s built-in Friendly URL Mapping functionality, and I hit a small snag which I thought I’d write about now.

I’m going to assume that you already know about Friendly URL Mapper in Liferay 6. If not then you should take a look at the Liferay Wiki page on it, or get the great Liferay In Action book, then come back here!

The Friendly URL Mapper is great. It’s quick and simple to set up, and hides a lot of the complexities of creating friendly URLs in Liferay. However it has one characteristic that I had to get rid of for my portlet. This was the Friendly URL Mapper prefix! If you have a look at the URL below between the two mentions of /wiki/ you’ll see what I’m talking about:

http://www.liferay.com/community/wiki/-/wiki/Main/FriendlyURLMapper

That /-/ is the Friendly URL Mapper prefix, and to my mind looks really bad! However I believe the reason behind it’s existence is also shown in the above URL. The prefix signifies the beginning of the Friendly URL, and the end of the actual URL. So you can have a page named the same as the as name of the mapping (as you can see the page above is “wiki” and the mapping is also “wiki”. With the code I’m showing you below, this won’t work so bare that in mind!

So there are two changes you need to make to your existing Friendly URL Mapping code to remove that prefix. The first is to create a new a class along the lines of MyPortletNameFriendlyURLMapper. This class needs to extend com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper. Then you all you need to do is override the isCheckMappingWithPrefix method of the BaseFriendlyURLMapper, by returning false. The code for this class can be seen below:

package com.mysmallcornerstudios.friendlierurlmapping.portlet;

import com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper;

public class FriendlierFriendlyURLMapper extends DefaultFriendlyURLMapper {

@Override
public boolean isCheckMappingWithPrefix() {

return false;

}

}

Then all we have to do is update the liferay-portlet.xml to say use our new FriendlyURLMapper class instead of the Default one, but the Default one is still doing most of the work for us. So in my liferay-portlet.xml I just change the <friendly-url-mapper-class> to the below:

<friendly-url-mapper-class>

com.mysmallcornerstudios.friendlierurlmapping.portlet.FriendlierFriendlyURLMapper

</friendly-url-mapper-class>

That’s it! Job done! So an example of what we’ve done here is change this URL:

http://mysite.com/somepage/-/friendlier/someParameter/

to:

http://mysite.com/somepage/friendlier/someParameter/

So it’s even friendlier to look at! The source code for this can be found in my GitHub repository called Liferay-6-Friendlier-Friendly-URL-Mapper. I’ve also added a WAR file to my repository so you can easily deploy this portlet. The WAR file can be found here.

I hope this helps people!

PowerShell: Creating & configuring a Active Directory user

This is a very bare bones PowerShell script for creating new users in Active Directory environment.

It can save huge amounts of time if you’re creating large numbers of new users as you can pipe values into the function like:

$firstNames = @("John","Jane")
$lastNames = @("Smith","Doe")

for ($i = 0; $i -lt $firstNames.Count; $i++) {
        Add-ADUser -FirstName $firstNames[$i] -LastName $lastNames[$i]
}

This basic skeleton can be changed to:

  • Have a different Login naming convention
  • Add more default groups
  • Add checks to change login names if it already exists

This will work with PowerShell version 1 and upwards.

If you have any questions, ask away in the comments section!

function Add-ADUser ($FirstName, $LastName) {

	$logon = $lastName + $firstName.subString(0,1)

	$domain = "DC=domainName,DC=com"

	# Bind to Users a particular OU
	$ou = [ADSI]"LDAP://OU=Users,$domain"

	# Create new user in AD
	$newUser = $ou.Create("user","CN=$lastName $firstName")

	# Bind common Mememberships
	$allUsers = [ADSI]"LDAP://CN=AllUsers,OU=Recipients,$domain"

	$groups = @($allUsers)

	# Modify new users details
	$newUser.put("sAMAccountName",$logon)
	$newUser.put("givenName",$firstName)
	$newUser.put("sn",$lastName)
	$newUser.put("DisplayName","$lastName $firstName")
	$newUser.put("userPrincipalName", $logon + "@domain.com")
	$newUser.put("profilePath","\\domainController\profiles\$logon")
	$newUser.SetInfo()

	$newUser.put("userAccountControl","512")
	$newUser.SetInfo()

	$newUser.psbase.Invoke("SetPassword","Password1")
	$newUser.psbase.CommitChanges()

	# Add user to required memberships

	foreach ($group in $groups) {
		$members = $group.member
		$group.member = $members + $newUser.distinguishedName
		$group.setInfo()
	}

        # Optional code for creating a Exchange Mailbox for the newly created user
	Enable-Mailbox -Identity "domain\$logon" `
		-Database "exchangeServer\Storage Group\Mailbox Database"
}

Powershell: Auditing Active Directory group

This is a bare bones Powershell script that is used for outputting the members of a Active Directory group, as well as the Managed By user.

You can pipe this to get a print out of multiple groups by doing:

“Group1″,”Group2″,”Group3″ | ForEach-Object { Audit-ADGroup -Group $_ }

This basic skeleton can be changed to work with:

  • A different LDAP / OU Structure
  • Output the groups in a more meaningful way
  • Add check to see if group exists

Add a comment if you have any questions or suggestions as to how you would improve this!

function Audit-ADGroup {
        param (
                [Parameter(mandatory=$true,Position=0)][ValidateNotNullOrEmpty()]
                [String]$Group
        )

        $grp = [ADSI]"LDAP://CN=$Group,OU=SomeOU,DC=SomeDomain,DC=com"

        [String]$managedBy = $grp.ManagedBy
        Write-Host "$Group " + $managedBy.Split(",")[0].Replace("CN=",""))

        $members = New-Object System.Collections.ArrayList

        foreach ($g in $grp.member) {
                $members.Add($g.Split(",")[0].Replace("CN=",""))
        }

        $members.Sort()

        $members | ForEach-Object {
                Write-Host $_
        }
}