Nibble

Archive for May, 2009

Displaying a random image/banner on your umbraco site 9

First some info on the setup:

After installing runway to get a basic site up and running I created a folder in the media section and added several images

randompicturemediajpg

On my homepage document type I added a ‘media picker’ property called ‘Pictures’. There I’ll select the media picture folder.

randompicturecontent

All that’s left now is to write some xslt that will select a random image from the media folder.

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE xsl:Stylesheet [ &lt;!ENTITY nbsp “&#x00A0;”> ]>
<xsl:stylesheet 
    version=”1.0″
    xmlns:msxsl=”urn:schemas-microsoft-com:xslt”  
    xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” 
    xmlns:msxml=”urn:schemas-microsoft-com:xslt”
    xmlns:umbraco.library=”urn:umbraco.library”
    xmlns:myFuncs=”urn:my-scripts” 
    exclude-result-prefixes=”msxml myFuncs umbraco.library”>
 
 
<xsl:output method=”xml” omit-xml-declaration=”yes” indent=”yes”/>
 
<xsl:param name=”currentPage”/>
 
<xsl:variable name=”imageRoot” 
    select=”$currentPage/ancestor-or-self:: node[@level = 1]/data [@alias = ‘Pictures’]”/>
 
<msxsl:script implements-prefix=”myFuncs” language=”JavaScript”> 
<![CDATA[
 
var myArray = new Array();
 
function random(){     
    var index = Math.floor(Math.random() * myArray.length);    
    return myArray [index];
}
 
function addtoArray(item){
    myArray.push(item);
}
 
]]>
 
</msxsl:script> 
 
<xsl:template match=”/”>
 
<!– start writing XSLT –>
 
<xsl:call-template name=”buildArray”>
<xsl:with-param name=”arrayNodes” 
    select=”umbraco.library:GetMedia($imageRoot, ‘true’)/node/data[@alias=’umbracoFile’]”/>
</xsl:call-template>
 
<xsl:value-of select=”myFuncs:random()”/>
 
</xsl:template>
 
<xsl:template name=”buildArray”>
<xsl:param name=”arrayNodes”/>
    <xsl:for-each select=”$arrayNodes”>
        <xsl:value-of select=”myFuncs:addtoArray(string(./text()))”/>
    </xsl:for-each>
</xsl:template>
 
</xsl:stylesheet>

This snippet will output the relative url of a random picture from inside the selected media folder ( like /media/44/dsc00033.jpg ).

RandomPicture xslt: download

Only log in umbraco members when they have been approved 14

A common thing when dealing with members on your site is that they need to be approved after registration before they can access the protected area on your site.

This post describes a simple way of doing that.

On the umbraco member type I have a True/false property with the alias active. If this is set to true on the member he is an ‘active’ member and is allowed to log in.

activeproperty

 

The only thing that is needed is to override the ValidateUser method of the UmbracoMembershipProvider. So make a custom class, inherit from umbraco.providers.members.UmbracoMembershipProvider and override the method (make sure to reference umbraco.providers).

Instead of just checking if there is a member with the supplied loginname and password we’ll add some extra code that will check if the member has his active property set to true. If that is the case we return true, in all other cases we return false.

 

namespace Nibble
{
    public class CustomMemberShipProvider : umbraco.providers.members.UmbracoMembershipProvider
    {
        public override bool ValidateUser(string username, string password)
        {
            umbraco.cms.businesslogic.member.Member m =
                umbraco.cms.businesslogic.member.Member.GetMemberFromLoginNameAndPassword(
                username, EncodePassword(password));
 
            if (m == null)
            {
                return false;
            }
            else
            {
                try
                {
                    if (m.getProperty(“active”).Value.ToString() == “1″)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
 
                }
                catch
                {
                    return false;
                }
             
            }
            return (m != null);
        }
    }
}

 

Final step is to modify the web.config so that our custom membership provider is used instead of the default umbraco one. So Just change the type attribute on the umbraco membership provider to the type of the custom membership provider.

<add name=”UmbracoMembershipProvider” type=”Nibble.CustomMemberShipProvider”
 enablePasswordRetrieval=”false” enablePasswordReset=”false” requiresQuestionAndAnswer=”false”
 defaultMemberTypeAlias=”CobraMember” passwordFormat=”Hashed” />

 

That’s it, now the asp:login control will only login the member if he has his active property set to true.