Skip to content

Commit

Permalink
Refactor setup to Initializer filters for menu setup and setting Curr…
Browse files Browse the repository at this point in the history
…entUser
  • Loading branch information
Maher Hawash committed Apr 16, 2012
1 parent 3681b41 commit 1e17240
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 107 deletions.
14 changes: 14 additions & 0 deletions Hometastic/Hometastic/Controllers/ApplicationController.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Imports Hometastic.Models
Namespace Hometastic
Public Class ApplicationController
Inherits System.Web.Mvc.Controller
Dim m_CurrentUser As MVNetBase = Nothing

Function CurrentUser()
If m_CurrentUser Is Nothing Then
m_CurrentUser = Session("CurrentUser")
End If
Return m_CurrentUser
End Function
End Class
End Namespace
8 changes: 0 additions & 8 deletions Hometastic/Hometastic/Controllers/HoaController.vb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ Namespace Hometastic
Public Class HoaController
Inherits System.Web.Mvc.Controller

Sub SetupMenu()
ViewBag.Menu = {({"My Account", "/Home/Index"}),
({"Services", "/Home/Services"}),
({"Contact Us", "/Home/ContactUs"})
}
End Sub
'
' GET: /Hoa

Function Index() As ActionResult
Expand Down Expand Up @@ -49,7 +42,6 @@ Namespace Hometastic
' GET: /Hoa/Edit/5

Function Edit(ByVal id As Integer) As ActionResult
SetupMenu()
Return View()
End Function

Expand Down
74 changes: 34 additions & 40 deletions Hometastic/Hometastic/Controllers/HomeController.vb
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
Imports System.Collections
Imports Hometastic.Models

Public Class HomeController
Inherits System.Web.Mvc.Controller
Sub SetupMenu()
ViewBag.Menu = {({"My Account", "/Home/Index"}),
({"Services", "/Home/Services"}),
({"Contact Us", "/Home/ContactUs"})
}
End Sub

Function Index(ByVal query As String) As ActionResult
Dim s As String = HttpContext.Request.RequestContext.RouteData.Values("path")
If s Is Nothing Then
s = "Index"
End If

SetupMenu()
Return View(s)
End Function

Public Function LogOn() As ActionResult
SetupMenu()
Return View("Index")
End Function
<HttpPost()> _
Public Function LogOn(ByVal collection As FormCollection) As ActionResult

If Account.Authenticate(collection("mgmtCoId"), collection("password"), collection("clientNumber"), collection("userType")) Then
Return RedirectToAction("Index", collection("userType"))
End If

Return RedirectToAction("LogOn")
End Function

<HttpPost()> _
Public Function ContactUs(ByVal collection As FormCollection) As ActionResult

Return View()
End Function

End Class
Namespace Hometastic
Public Class HomeController
Inherits ApplicationController

Function Index(ByVal query As String) As ActionResult
Dim s As String = HttpContext.Request.RequestContext.RouteData.Values("path")
If s Is Nothing Then
s = "Index"
End If

Return View(s)
End Function

Public Function LogOn() As ActionResult
Return View("Index")
End Function
<HttpPost()> _
Public Function LogOn(ByVal collection As FormCollection) As ActionResult

If Account.Authenticate(collection("mgmtCoId"), collection("password"), collection("clientNumber"), collection("userType")) Then
Return RedirectToAction("Index", collection("userType"))
End If

Return RedirectToAction("LogOn")
End Function

<HttpPost()> _
Public Function ContactUs(ByVal collection As FormCollection) As ActionResult

Return View()
End Function

End Class
End Namespace
19 changes: 1 addition & 18 deletions Hometastic/Hometastic/Controllers/ManagementCompanyController.vb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,11 @@ Imports System.Text

Namespace Hometastic
Public Class ManagementCompanyController
Inherits System.Web.Mvc.Controller

Dim CurrentUser As ManagementCompanyUser

Sub SetupMenu()
ViewBag.Menu = {({"My Account", "/ManagementCompany/Index"}),
({"Manage Site", "/ManagementCompany/Edit"}),
({"News", "/News/Index"}),
({"Q&A", "/ManagementCompany/Survey"})
}

Account.Authenticate("6400", "pmsi", "", "ManagementCompany")
CurrentUser = Session("CurrentUser")
End Sub
Inherits ApplicationController
'
' GET: /ManagementCompany

Function Index() As ActionResult
SetupMenu()
ViewBag.VendorList = CurrentUser.VendorList()
ViewBag.HoaList = CurrentUser.HoaList()
Return View(CurrentUser)
Expand All @@ -35,7 +21,6 @@ Namespace Hometastic

Function Edit() As ActionResult
Account.Authenticate("6400", "pmsi", "", "ManagementCompany")
SetupMenu()
Return View(CurrentUser)
End Function

Expand All @@ -45,7 +30,6 @@ Namespace Hometastic
<HttpPost()> _
Function Edit(ByVal collection As FormCollection) As ActionResult
Try
SetupMenu()
CurrentUser.Write(collection)
Return RedirectToAction("Index")
Catch
Expand All @@ -56,7 +40,6 @@ Namespace Hometastic
<HttpPost()> _
Function Upload(ByVal collection As FormCollection) As ActionResult
Dim writer As FileStream
SetupMenu()

' TODO: Delete exisiting images of same pattern.

Expand Down
26 changes: 4 additions & 22 deletions Hometastic/Hometastic/Controllers/NewsController.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,10 @@ Imports BlueFinity.mvNET.CoreObjects

Namespace Hometastic
Public Class NewsController
Inherits System.Web.Mvc.Controller
Dim CurrentUser As MVNetBase

Sub SetupMenu()
ViewBag.Menu = {({"My Account", "/ManagementCompany/Index"}),
({"Manage Site", "/ManagementCompany/Edit"}),
({"News", "/News/Index"}),
({"Q&A", "/ManagementCompany/Survey"})
}

Account.Authenticate("6400", "pmsi", "", "ManagementCompany")
CurrentUser = Session("CurrentUser")
End Sub
Inherits ApplicationController
'
' GET: /News

Function Index() As ActionResult
SetupMenu()
ViewBag.NewsList = CurrentUser.NewsList
Return View()
End Function
Expand All @@ -29,7 +15,6 @@ Namespace Hometastic
' GET: /News/Create

Function Create() As ActionResult
SetupMenu()
Return View(New News())
End Function

Expand All @@ -39,8 +24,7 @@ Namespace Hometastic
<HttpPost()> _
Function Create(ByVal collection As FormCollection) As ActionResult
Try
SetupMenu()
Dim newsItem = New News(CurrentUser)
Dim newsItem = New News(CType(CurrentUser(), ManagementCompanyUser))
newsItem.Write(collection)
Return RedirectToAction("Index")
Catch
Expand All @@ -52,8 +36,7 @@ Namespace Hometastic
' GET: /News/Edit/5

Function Edit(ByVal id As String) As ActionResult
SetupMenu()
Dim newsItem = New News(CurrentUser)
Dim newsItem = New News(CType(CurrentUser(), ManagementCompanyUser))
newsItem.Read(id.Replace("_", "*")) ' Well the browser does not like splat(*), so we pacifiy it.
Return View(newsItem)
End Function
Expand All @@ -64,8 +47,7 @@ Namespace Hometastic
<HttpPost()> _
Function Edit(ByVal id As String, ByVal collection As FormCollection) As ActionResult
Try
SetupMenu()
Dim newsItem = New News(CurrentUser)
Dim newsItem = New News(CType(CurrentUser(), ManagementCompanyUser))
newsItem.Read(id.Replace("_", "*")) ' Well the browser does not like splat(*), so we pacifiy it.
newsItem.Write(collection)
Return RedirectToAction("Index")
Expand Down
8 changes: 0 additions & 8 deletions Hometastic/Hometastic/Controllers/VendorController.vb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ Namespace Hometastic
Public Class VendorController
Inherits System.Web.Mvc.Controller

Sub SetupMenu()
ViewBag.Menu = {({"My Account", "/Home/Index"}),
({"Services", "/Home/Services"}),
({"Contact Us", "/Home/ContactUs"})
}
End Sub
'
' GET: /Hoa

Function Index() As ActionResult
Expand Down Expand Up @@ -49,7 +42,6 @@ Namespace Hometastic
' GET: /Hoa/Edit/5

Function Edit(ByVal id As Integer) As ActionResult
SetupMenu()
Return View()
End Function

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Imports System.Web
Imports System.Web.Mvc
Imports Hometastic.Models

Public Class ActionFilterProvider
Public Class FilterProvider
Implements IFilterProvider

Private actions As IList(Of ControllerAction) = New List(Of ControllerAction)

Public Sub Add(ByVal controllername As String, ByVal actionname As String, ByVal filter As AuthorizeAttribute)
Public Sub Add(ByVal controllername As String, ByVal actionname As String, ByVal filter As FilterAttribute)
actions.Add(New ControllerAction With
{.ControllerName = controllername,
.ActionName = actionname,
Expand Down Expand Up @@ -38,8 +38,7 @@ Public Class ActionFilterProvider
End Class

Friend Class ControllerAction

Friend Property ControllerName As String
Friend Property ActionName As String
Friend Property filter As AuthorizeAttribute
End Class
Friend Property filter As FilterAttribute
End Class
33 changes: 33 additions & 0 deletions Hometastic/Hometastic/Filters/InitializeFilters.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Imports MvcFlash.Core

Namespace Models
' This filter limits controller actions to users who log in as a management company.
' In web.config, the section defines where it should redirect on failed authentication:
' <authentication mode="Forms">
' <forms defaultUrl="/" loginUrl="/" timeout="2880" />
' </authentication>
' The filters are attached to the controller through the ActionFilterProvider class in global.asax .
'
Public Class ManagementCompanyInitializeFilter
Inherits ActionFilterAttribute
Public Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)
filterContext.Controller.ViewBag.Menu = {({"My Account", "/ManagementCompany/Index"}),
({"Manage Site", "/ManagementCompany/Edit"}),
({"News", "/News/Index"}),
({"Q&A", "/ManagementCompany/Survey"})
}
Account.Authenticate("6400", "pmsi", "", "ManagementCompany")
End Sub
End Class

Public Class HoaInitializeFilter
Inherits ActionFilterAttribute
Public Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)
filterContext.Controller.ViewBag.Menu = {({"My Account", "/ManagementCompany/Index"}),
({"Manage Site", "/ManagementCompany/Edit"}),
({"News", "/News/Index"}),
({"This and That", "/ManagementCompany/Survey"})
}
End Sub
End Class
End Namespace
9 changes: 7 additions & 2 deletions Hometastic/Hometastic/Global.asax.vb
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
' Note: For instructions on enabling IIS6 or IIS7 classic mode,
' visit http://go.microsoft.com/?LinkId=9394802

Imports Hometastic.Models

Public Class MvcApplication
Inherits System.Web.HttpApplication

Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
filters.Add(New HandleErrorAttribute())
'Dim provider As New ActionFilterProvider
Dim provider As New FilterProvider
'provider.Add("ManagementCompany", "*", New ManagementUserFilter)
'provider.Add("Hoa", "*", New HoaUserFilter)
'FilterProviders.Providers.Add(provider)

Provider.Add("ManagementCompany", "*", New ManagementCompanyInitializeFilter)
provider.Add("Home", "*", New ManagementCompanyInitializeFilter)
provider.Add("News", "*", New ManagementCompanyInitializeFilter)
provider.Add("Hoa", "*", New HoaInitializeFilter)
FilterProviders.Providers.Add(provider)
End Sub

Shared Sub MapResource(ByVal resource As String)
Expand Down
6 changes: 4 additions & 2 deletions Hometastic/Hometastic/Hometastic.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@
<Import Include="System.Web.UI.HtmlControls" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\ApplicationController.vb" />
<Compile Include="Controllers\NewsController.vb" />
<Compile Include="Controllers\VendorController.vb" />
<Compile Include="Controllers\HoaController.vb" />
<Compile Include="Controllers\ManagementCompanyController.vb" />
<Compile Include="Filters\ActionFilterProvider.vb" />
<Compile Include="Filters\LoginFilters.vb" />
<Compile Include="Filters\FilterProvider.vb" />
<Compile Include="Filters\InitializeFilters.vb" />
<Compile Include="Filters\AuthenticationFilters.vb" />
<Compile Include="Global.asax.vb">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
Expand Down
1 change: 0 additions & 1 deletion Hometastic/Hometastic/Models/MVNetBase.vb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Imports BlueFinity.mvNET.CoreObjects

Namespace Models

Public Class MVNetBase
Protected Friend m_mvAccount As mvAccount
Protected Friend m_TableName As String = Nothing
Expand Down
2 changes: 1 addition & 1 deletion Hometastic/Hometastic/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<add key="HometasticInfoFolder" value="C:\src\Hometast3Info"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
Expand Down

0 comments on commit 1e17240

Please sign in to comment.