bug 1801 : Better understanding of how 'Assert...' methods are supposed to work, leading to simpler code.

This commit is contained in:
sb 2008-04-17 17:33:16 +00:00
parent fd3bfabe3a
commit 4efb909880

View file

@ -9,8 +9,8 @@
Transform ADL into (partial) controller classes Transform ADL into (partial) controller classes
$Author: sb $ $Author: sb $
$Revision: 1.20 $ $Revision: 1.21 $
$Date: 2008-04-17 16:16:19 $ $Date: 2008-04-17 17:33:16 $
--> -->
<!-- WARNING WARNING WARNING: Do NOT reformat this file! <!-- WARNING WARNING WARNING: Do NOT reformat this file!
@ -71,7 +71,7 @@
// //
// Controller for auto-generated forms for editing <xsl:value-of select="@name"/>s // Controller for auto-generated forms for editing <xsl:value-of select="@name"/>s
// Automatically generated from application description using // Automatically generated from application description using
// adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.20 $', 10)"/> // adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.21 $', 10)"/>
// //
// This file is automatically generated; DO NOT EDIT IT. // This file is automatically generated; DO NOT EDIT IT.
// //
@ -128,7 +128,7 @@ namespace <xsl:value-of select="$controllerns"/> {
// //
// Controller for auto-generated forms for editing <xsl:value-of select="@name"/>s // Controller for auto-generated forms for editing <xsl:value-of select="@name"/>s
// Automatically generated from application description using // Automatically generated from application description using
// adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.20 $', 10)"/> // adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.21 $', 10)"/>
// //
// This file is automatically generated; DO NOT EDIT IT. // This file is automatically generated; DO NOT EDIT IT.
// //
@ -194,8 +194,8 @@ namespace <xsl:value-of select="$controllerns"/> {
</xsl:call-template> </xsl:call-template>
if ( record == null) { if ( record == null) {
if ( AssertUserCanCreate()) {
/* it seems to be new, create persistent object */ /* it seems to be new, create persistent object */
AssertUserCanCreate()
try { try {
record = new <xsl:value-of select="concat($entityns, '.', @name)"/>(<xsl:for-each select="adl:key/adl:property"> record = new <xsl:value-of select="concat($entityns, '.', @name)"/>(<xsl:for-each select="adl:key/adl:property">
<xsl:variable name="basetype"> <xsl:variable name="basetype">
@ -240,15 +240,14 @@ namespace <xsl:value-of select="$controllerns"/> {
messages.Add( "New <xsl:value-of select="@name"/> record created"); messages.Add( "New <xsl:value-of select="@name"/> record created");
isnewborn = true; isnewborn = true;
} }
else
{
AddError( "You are not authorised to create objects of type <xsl:value-of select="@name"/>");
}
}
if ( record != null) { if ( record != null) {
if ( isnewborn || AssertUserCanUpdate()) if ( ! isnewborn) {
{ // isnewborn cannot be true unless we've already checked user can create
// so no need to do it again here
AssertUserCanUpdate();
}
try { try {
/* actually update the record */ /* actually update the record */
BindObjectInstance( record, ParamStore.Form, "instance"); BindObjectInstance( record, ParamStore.Form, "instance");
@ -380,8 +379,6 @@ namespace <xsl:value-of select="$controllerns"/> {
hibernator.Flush(); hibernator.Flush();
messages.Add( "Record saved successfully"); messages.Add( "Record saved successfully");
} }
catch ( DataSuitabilityException dse) catch ( DataSuitabilityException dse)
{ {
@ -391,10 +388,6 @@ namespace <xsl:value-of select="$controllerns"/> {
{ {
AddError( axe.Message); AddError( axe.Message);
} }
}
else {
AddError( "You are not authorised to update objects of type <xsl:value-of select="@name"/>");
}
PropertyBag["messages"] = messages; PropertyBag["messages"] = messages;
PropertyBag["instance"] = record; PropertyBag["instance"] = record;
@ -433,8 +426,7 @@ namespace <xsl:value-of select="$controllerns"/> {
[AccessibleThrough(Verb.Post)] [AccessibleThrough(Verb.Post)]
public void Delete() public void Delete()
{ {
if ( AssertUserCanDelete()) AssertUserCanDelete();
{
ISession hibernator = ISession hibernator =
NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN], NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN],
Session[NHibernateHelper.PASSTOKEN]</xsl:if>); Session[NHibernateHelper.PASSTOKEN]</xsl:if>);
@ -466,9 +458,6 @@ namespace <xsl:value-of select="$controllerns"/> {
Redirect( FormsAuthentication.DefaultUrl); Redirect( FormsAuthentication.DefaultUrl);
</xsl:otherwise> </xsl:otherwise>
</xsl:choose> </xsl:choose>
} else {
AddError( "You are not authorised to delete objects of type <xsl:value-of select="@name"/>");
}
} }
</xsl:if> </xsl:if>
<xsl:apply-templates select="adl:form"/> <xsl:apply-templates select="adl:form"/>
@ -492,7 +481,7 @@ namespace <xsl:value-of select="$controllerns"/> {
/// &lt;param name="view"&gt;The name of the list view to show&lt;/param&gt; /// &lt;param name="view"&gt;The name of the list view to show&lt;/param&gt;
public void InternalShowList( String view) public void InternalShowList( String view)
{ {
if ( AssertUserCanRead()) { AssertUserCanRead();
ISession hibernator = ISession hibernator =
NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN], NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN],
Session[NHibernateHelper.PASSTOKEN]</xsl:if>); Session[NHibernateHelper.PASSTOKEN]</xsl:if>);
@ -508,9 +497,6 @@ namespace <xsl:value-of select="$controllerns"/> {
PaginationHelper.CreatePagination( this, instances, 25); PaginationHelper.CreatePagination( this, instances, 25);
RenderViewWithFailover(view + ".vm", view + ".auto.vm"); RenderViewWithFailover(view + ".vm", view + ".auto.vm");
} else {
AddError( "You are not authorised to delete view of type <xsl:value-of select="@name"/>");
}
} }
</xsl:if> </xsl:if>
} }
@ -604,6 +590,8 @@ namespace <xsl:value-of select="$controllerns"/> {
[AccessibleThrough(Verb.Get)] [AccessibleThrough(Verb.Get)]
public void <xsl:value-of select="@name"/>( ) public void <xsl:value-of select="@name"/>( )
{ {
AssertUserCanRead();
ISession hibernator = ISession hibernator =
NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN], NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN],
Session[NHibernateHelper.PASSTOKEN]</xsl:if>); Session[NHibernateHelper.PASSTOKEN]</xsl:if>);