Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting code for Web API implementation #268

Merged
merged 13 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ActiveForumViewerSettings.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void LoadSettings()
}
catch (Exception exc)
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
}
}
public override void UpdateSettings()
Expand Down Expand Up @@ -119,7 +119,7 @@ public override void UpdateSettings()
}
catch (Exception exc)
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
}
}
public void LoadForums()
Expand All @@ -131,7 +131,7 @@ public void LoadForums()
Entities.Tabs.TabInfo ti;
foreach (Entities.Modules.ModuleInfo mi in mc.GetModules(PortalId))
{
if (mi.DesktopModule.ModuleName.Trim().ToLowerInvariant() == "Active Forums".ToLowerInvariant() && mi.IsDeleted == false)
if (mi.DesktopModule.ModuleName.Trim().ToLowerInvariant() == Globals.ModuleName.ToLowerInvariant() && mi.IsDeleted == false)
{
ti = tc.GetTab(mi.TabID, PortalId, false);
if (ti != null)
Expand Down
8 changes: 6 additions & 2 deletions ActiveForums.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Reference Include="Microsoft.ApplicationBlocks.Data, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\DotNetNuke.Core.9.4.0\lib\net45\Microsoft.ApplicationBlocks.Data.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\microsoft-web-helpers.2.1.20710.2\lib\net40\Microsoft.Web.Helpers.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -771,6 +772,9 @@
<Compile Include="DAL2\Reply\Reply.cs" />
<Compile Include="DAL2\Reply\ReplyController.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Services\Controllers\ControllerBase.cs" />
<Compile Include="Services\ForumAuthorizeAttribute.cs" />
<Compile Include="Services\ServicesHelper.cs" />
<Compile Include="SimulateDateDiff.cs" />
<Compile Include="SimulateIsNumeric.cs" />
<Compile Include="ActiveForums.ascx.designer.cs">
Expand Down Expand Up @@ -994,7 +998,7 @@
</Compile>
<Compile Include="Providers\DataProviders\SqlDataProvider\SqlDataProvider.cs" />
<Compile Include="Services\AdminServiceController.cs" />
<Compile Include="Services\ForumRouteMapper.cs" />
<Compile Include="Services\RouteMapper.cs" />
<Compile Include="Services\ForumServiceController.cs" />
<Compile Include="Services\ModerationService.cs" />
<Compile Include="ForumSettings.ascx.designer.cs">
Expand Down Expand Up @@ -1380,4 +1384,4 @@
<!--<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />-->
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<Import Project="Module.Build" />
</Project>
</Project>
8 changes: 4 additions & 4 deletions ControlPanel.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected override void OnLoad(EventArgs e)
}
catch (Exception ex)
{
Services.Exceptions.Exceptions.LogException(ex);
DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
}
}
bool loadDefault = true;
Expand Down Expand Up @@ -116,7 +116,7 @@ protected override void OnLoad(EventArgs e)
}
else
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
}
}

Expand Down Expand Up @@ -181,7 +181,7 @@ public void cbShell_Callback(object sender, Controls.CallBackEventArgs e)
}
else
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ private void GetControl(string view, string options, bool IsCallback)
}
else
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
}
}

Expand Down
2 changes: 1 addition & 1 deletion CustomControls/ServerControls/UserStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected override void RenderContents(HtmlTextWriter writer)
DotNetNuke.Entities.Tabs.TabInfo ti = null;
foreach (DotNetNuke.Entities.Modules.ModuleInfo mi in mc.GetModules(portalId))
{
if (mi.DesktopModule.ModuleName.ToUpperInvariant() == "Active Forums".ToUpperInvariant())
if (mi.DesktopModule.ModuleName.ToUpperInvariant() == Globals.ModuleName.ToUpperInvariant())
{
ModuleId = mi.ModuleID;
break;
Expand Down
10 changes: 5 additions & 5 deletions CustomControls/UserControls/ForumView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ protected override void OnInit(EventArgs e)
}
catch (Exception ex)
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
}
}
}
catch (Exception ex)
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
}
}

Expand Down Expand Up @@ -201,7 +201,7 @@ public string BuildForumView(int ForumTemplateId, int CurrentUserId, string Them
}
catch (Exception ex)
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
sTemplate = ex.Message; //ParseControls(sTemplate)
}
if (sTemplate.Contains("[NOTOOLBAR]"))
Expand Down Expand Up @@ -362,7 +362,7 @@ public string BuildForumView(int ForumTemplateId, int CurrentUserId, string Them
}
catch (Exception ex)
{
Services.Exceptions.Exceptions.LogException(ex);
DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
return string.Empty;
}
}
Expand Down Expand Up @@ -416,7 +416,7 @@ private Forum FillForumRow(DataRow dr)
}
catch (Exception ex)
{
Services.Exceptions.Exceptions.LogException(ex);
DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
}

return fi;
Expand Down
6 changes: 3 additions & 3 deletions ForumSettings.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public override void LoadSettings()
}
catch (Exception exc) //Module failed to load
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
}
}

Expand Down Expand Up @@ -273,7 +273,7 @@ public override void UpdateSettings()
catch (Exception ex)
{
FullTextSearch = false;
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
}

// Clear out the cache
Expand All @@ -282,7 +282,7 @@ public override void UpdateSettings()
}
catch (Exception exc) //Module failed to load
{
Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Services/AdminServiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace DotNetNuke.Modules.ActiveForums
{
[ValidateAntiForgeryToken]
[SupportedModules("Active Forums")]
[SupportedModules(Globals.ModuleName)]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Edit)]
public class AdminServiceController : DnnApiController
{
Expand Down
49 changes: 49 additions & 0 deletions Services/Controllers/ControllerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Community Forums
// Copyright (c) 2013-2021
// by DNN Community
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DotNetNuke.Web.Api;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Instrumentation;

namespace DotNetNuke.Modules.ActiveForums.Services
{
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <typeparam name="T"></typeparam>
[SupportedModules(Globals.ModuleName)] /* this MUST match DesktopModule.ModuleName so use new constant */
public class ControllerBase<T> : DnnApiController
{
private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(T));
/// <summary>
/// provide a simple method for testing
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage HelloWorld()
{
Logger.Info("Hello World!");
return Request.CreateResponse(HttpStatusCode.OK, "Hello World!");
}
}
}
114 changes: 114 additions & 0 deletions Services/ForumAuthorizeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// Community Forums
// Copyright (c) 2013-2021
// by DNN Community
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
using System;
using System.Linq;
using System.Net.Http;
using System.Security.Principal;
using System.Threading;
using System.Web.Helpers;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Framework;
using DotNetNuke.Web.Api;
namespace DotNetNuke.Modules.ActiveForums.Services
{
/// <summary>
/// Use this attribute to set which forum permission is required to perform an API action.
/// The forumId included in the API URL will be used to verify that the action can be performed based on the permissions for the forum.
/// </summary>
public sealed class ForumsAuthorizeAttribute : AuthorizeAttributeBase, IOverrideDefaultAuthLevel
{
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="permissionNeeded"></param>
public ForumsAuthorizeAttribute(SecureActions permissionNeeded)
{
PermissionNeeded = permissionNeeded;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public SecureActions PermissionNeeded { get; }
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
/// <exception cref="System.InvalidOperationException"></exception>
/// <remarks>System.InvalidOperationException is thrown if you specify this attribute on an API method that does not include forumId</remarks>
public override bool IsAuthorized(AuthFilterContext context)
{
Requires.NotNull("context", context);
IIdentity identity = Thread.CurrentPrincipal.Identity;
if (identity.IsAuthenticated)
{
int forumId = -1;
if (forumId <= 0)
{
if (context.ActionContext.Request.GetRouteData().Route.DataTokens.ContainsKey("forumId"))
{
forumId = Utilities.SafeConvertInt(context.ActionContext.Request.GetRouteData().Route
.DataTokens["forumId"]
.ToString());
}
}
if (forumId <= 0)
{
try
{
forumId = Utilities.SafeConvertInt(context.ActionContext.Request.GetQueryNameValuePairs()
.Where(q => q.Key.ToLowerInvariant() == "forumid")
.FirstOrDefault().Value);
}
catch
{
}
}
if (forumId <= 0)
{
try
{
var postData = context.ActionContext.Request.Content.ReadAsStringAsync().Result;
forumId = System.Web.Helpers.Json.Decode(postData).ForumId;
}
catch
{
}
}
if (forumId <= 0)
{
throw new System.InvalidOperationException();
}
else
{
ModuleInfo moduleInfo = context.ActionContext.Request.FindModuleInfo();
PortalSettings portal = ServiceLocator<IPortalController, PortalController>.Instance.GetCurrentPortalSettings();
UserInfo userInfo = ServiceLocator<IPortalController, PortalController>.Instance.GetCurrentPortalSettings().UserInfo;
return ServicesHelper.IsAuthorized(portal.PortalId, moduleInfo.ModuleID, forumId, PermissionNeeded, userInfo);
}
}
return false;
}
}
}
13 changes: 10 additions & 3 deletions Services/ForumRouteMapper.cs → Services/RouteMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
//
using DotNetNuke.Web.Api;

namespace DotNetNuke.Modules.ActiveForums
namespace DotNetNuke.Modules.ActiveForums.Services
{
public class ForumRouteMapper : IServiceRouteMapper
/// <summary>
/// <inheritdoc/>
/// </summary>
public class RouteMapper : IServiceRouteMapper
{
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="mapRouteManager"></param>
public void RegisterRoutes(IMapRoute mapRouteManager)
{
mapRouteManager.MapHttpRoute("ActiveForums", "default", "{controller}/{action}", new {}, new[] { "DotNetNuke.Modules.ActiveForums" });
mapRouteManager.MapHttpRoute("ActiveForums", "default", "{controller}/{action}", new { }, new[] { "DotNetNuke.Modules.ActiveForums", "DotNetNuke.Modules.ActiveForums.Services.Controllers" });
}
}
}
Expand Down
Loading