Skip to content

Commit 522ec88

Browse files
committed
Resolves #417 Restrict forum groups to specific customer roles
1 parent 8039e3f commit 522ec88

File tree

12 files changed

+362
-92
lines changed

12 files changed

+362
-92
lines changed

changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* Added option to display preview pictures in product lists
1616
* Added option to add multiple file versions to product download section
1717
* Added options for alternating price display (in badges)
18-
* Added option to display a captcha on forum pages when creating or replying to a topic.
18+
* **Forum**:
19+
* Added option to display a captcha on forum pages when creating or replying to a topic.
20+
* #417 Restrict forum groups to specific customer roles.
1921
* **MegaSearch**:
2022
* Supports searching for forum posts.
2123
* #1172 Option to display related search terms on search page.

src/Libraries/SmartStore.Core/Domain/Forums/ForumGroup.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using SmartStore.Core.Domain.Localization;
4+
using SmartStore.Core.Domain.Security;
45
using SmartStore.Core.Domain.Seo;
56
using SmartStore.Core.Domain.Stores;
67

@@ -9,7 +10,7 @@ namespace SmartStore.Core.Domain.Forums
910
/// <summary>
1011
/// Represents a forum group
1112
/// </summary>
12-
public partial class ForumGroup : BaseEntity, IAuditable, IStoreMappingSupported, ILocalizedEntity, ISlugSupported
13+
public partial class ForumGroup : BaseEntity, IAuditable, IStoreMappingSupported, IAclSupported, ILocalizedEntity, ISlugSupported
1314
{
1415
private ICollection<Forum> _forums;
1516

@@ -43,6 +44,11 @@ public partial class ForumGroup : BaseEntity, IAuditable, IStoreMappingSupported
4344
/// </summary>
4445
public bool LimitedToStores { get; set; }
4546

47+
/// <summary>
48+
/// Gets or sets a value indicating whether the entity is subject to ACL
49+
/// </summary>
50+
public bool SubjectToAcl { get; set; }
51+
4652
/// <summary>
4753
/// Gets or sets the collection of Forums
4854
/// </summary>

src/Libraries/SmartStore.Data/Migrations/201809241947314_ForumGroupAcl.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace SmartStore.Data.Migrations
2+
{
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
6+
public partial class ForumGroupAcl : DbMigration
7+
{
8+
public override void Up()
9+
{
10+
AddColumn("dbo.Forums_Group", "SubjectToAcl", c => c.Boolean(nullable: false));
11+
}
12+
13+
public override void Down()
14+
{
15+
DropColumn("dbo.Forums_Group", "SubjectToAcl");
16+
}
17+
}
18+
}

src/Libraries/SmartStore.Data/Migrations/201809241947314_ForumGroupAcl.resx

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

src/Libraries/SmartStore.Data/SmartStore.Data.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,10 @@
623623
<Compile Include="Migrations\201809171309522_NewsletterSubscriptionLanguage.Designer.cs">
624624
<DependentUpon>201809171309522_NewsletterSubscriptionLanguage.cs</DependentUpon>
625625
</Compile>
626+
<Compile Include="Migrations\201809241947314_ForumGroupAcl.cs" />
627+
<Compile Include="Migrations\201809241947314_ForumGroupAcl.Designer.cs">
628+
<DependentUpon>201809241947314_ForumGroupAcl.cs</DependentUpon>
629+
</Compile>
626630
<Compile Include="ObjectContextBase.SaveChanges.cs" />
627631
<Compile Include="Setup\Builder\ActivityLogTypeMigrator.cs" />
628632
<Compile Include="Setup\Builder\PermissionMigrator.cs" />
@@ -1120,6 +1124,9 @@
11201124
<EmbeddedResource Include="Migrations\201809171309522_NewsletterSubscriptionLanguage.resx">
11211125
<DependentUpon>201809171309522_NewsletterSubscriptionLanguage.cs</DependentUpon>
11221126
</EmbeddedResource>
1127+
<EmbeddedResource Include="Migrations\201809241947314_ForumGroupAcl.resx">
1128+
<DependentUpon>201809241947314_ForumGroupAcl.cs</DependentUpon>
1129+
</EmbeddedResource>
11231130
<EmbeddedResource Include="Sql\Indexes.sql" />
11241131
<EmbeddedResource Include="Sql\StoredProcedures.sql" />
11251132
</ItemGroup>

src/Libraries/SmartStore.Services/Forums/ForumService.cs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SmartStore.Core.Data;
55
using SmartStore.Core.Domain.Customers;
66
using SmartStore.Core.Domain.Forums;
7+
using SmartStore.Core.Domain.Security;
78
using SmartStore.Core.Domain.Stores;
89
using SmartStore.Data.Caching;
910
using SmartStore.Services.Common;
@@ -19,6 +20,7 @@ public partial class ForumService : IForumService
1920
private readonly IRepository<ForumPost> _forumPostRepository;
2021
private readonly IRepository<PrivateMessage> _forumPrivateMessageRepository;
2122
private readonly IRepository<ForumSubscription> _forumSubscriptionRepository;
23+
private readonly IRepository<AclRecord> _aclRepository;
2224
private readonly ForumSettings _forumSettings;
2325
private readonly IRepository<Customer> _customerRepository;
2426
private readonly IGenericAttributeService _genericAttributeService;
@@ -33,6 +35,7 @@ public ForumService(
3335
IRepository<ForumPost> forumPostRepository,
3436
IRepository<PrivateMessage> forumPrivateMessageRepository,
3537
IRepository<ForumSubscription> forumSubscriptionRepository,
38+
IRepository<AclRecord> aclRepository,
3639
ForumSettings forumSettings,
3740
IRepository<Customer> customerRepository,
3841
IGenericAttributeService genericAttributeService,
@@ -46,6 +49,7 @@ public ForumService(
4649
_forumPostRepository = forumPostRepository;
4750
_forumPrivateMessageRepository = forumPrivateMessageRepository;
4851
_forumSubscriptionRepository = forumSubscriptionRepository;
52+
_aclRepository = aclRepository;
4953
_forumSettings = forumSettings;
5054
_customerRepository = customerRepository;
5155
_genericAttributeService = genericAttributeService;
@@ -146,8 +150,9 @@ public virtual ForumGroup GetForumGroupById(int forumGroupId)
146150
return _forumGroupRepository.GetById(forumGroupId);
147151
}
148152

149-
public virtual IList<ForumGroup> GetAllForumGroups(int storeId = 0)
153+
public virtual IList<ForumGroup> GetAllForumGroups(int storeId = 0, bool showHidden = false)
150154
{
155+
var joinApplied = false;
151156
var query = _forumGroupRepository.Table.Expand(x => x.Forums);
152157

153158
if (!QuerySettings.IgnoreMultiStore && storeId > 0)
@@ -159,6 +164,25 @@ from sm in fg_sm.DefaultIfEmpty()
159164
where !fg.LimitedToStores || storeId == sm.StoreId
160165
select fg;
161166

167+
joinApplied = true;
168+
}
169+
170+
if (!showHidden && !QuerySettings.IgnoreAcl)
171+
{
172+
var allowedCustomerRolesIds = _services.WorkContext.CurrentCustomer.CustomerRoles.Where(x => x.Active).Select(x => x.Id).ToList();
173+
174+
query =
175+
from fg in query
176+
join a in _aclRepository.Table on new { a1 = fg.Id, a2 = "ForumGroup" } equals new { a1 = a.EntityId, a2 = a.EntityName } into fg_acl
177+
from a in fg_acl.DefaultIfEmpty()
178+
where !fg.SubjectToAcl || allowedCustomerRolesIds.Contains(a.CustomerRoleId)
179+
select fg;
180+
181+
joinApplied = true;
182+
}
183+
184+
if (joinApplied)
185+
{
162186
query =
163187
from fg in query
164188
group fg by fg.Id into fgGroup
@@ -317,8 +341,9 @@ public virtual IPagedList<ForumTopic> GetAllTopics(int forumId, int pageIndex, i
317341
return topics;
318342
}
319343

320-
public virtual IList<ForumTopic> GetActiveTopics(int forumId, int count)
344+
public virtual IList<ForumTopic> GetActiveTopics(int forumId, int count, bool showHidden = false)
321345
{
346+
var joinApplied = false;
322347
var query =
323348
from ft in _forumTopicRepository.Table
324349
where (forumId == 0 || ft.ForumId == forumId) && (ft.LastPostTime.HasValue)
@@ -337,14 +362,35 @@ from sm in fg_sm.DefaultIfEmpty()
337362
where !fg.LimitedToStores || currentStoreId == sm.StoreId
338363
select ft;
339364

340-
query =
341-
from ft in query
342-
group ft by ft.Id into ftGroup
343-
orderby ftGroup.Key
344-
select ftGroup.FirstOrDefault();
365+
joinApplied = true;
345366
}
346367

347-
query = query.OrderByDescending(x => x.LastPostTime);
368+
if (!showHidden && !QuerySettings.IgnoreAcl)
369+
{
370+
var allowedCustomerRolesIds = _services.WorkContext.CurrentCustomer.CustomerRoles.Where(x => x.Active).Select(x => x.Id).ToList();
371+
372+
query =
373+
from ft in query
374+
join ff in _forumRepository.Table on ft.ForumId equals ff.Id
375+
join fg in _forumGroupRepository.Table on ff.ForumGroupId equals fg.Id
376+
join a in _aclRepository.Table on new { a1 = fg.Id, a2 = "ForumGroup" } equals new { a1 = a.EntityId, a2 = a.EntityName } into fg_acl
377+
from a in fg_acl.DefaultIfEmpty()
378+
where !fg.SubjectToAcl || allowedCustomerRolesIds.Contains(a.CustomerRoleId)
379+
select ft;
380+
381+
joinApplied = true;
382+
}
383+
384+
if (joinApplied)
385+
{
386+
query =
387+
from ft in query
388+
group ft by ft.Id into ftGroup
389+
orderby ftGroup.Key
390+
select ftGroup.FirstOrDefault();
391+
}
392+
393+
query = query.OrderByDescending(x => x.LastPostTime);
348394

349395
var forumTopics = query.Take(count).ToList();
350396
return forumTopics;

src/Libraries/SmartStore.Services/Forums/IForumService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public partial interface IForumService
2323
/// Gets all forum groups
2424
/// </summary>
2525
/// <param name="storeId">Store identifier</param>
26+
/// <param name="showHidden">Whether to load hidden records</param>
2627
/// <returns>Forum groups</returns>
27-
IList<ForumGroup> GetAllForumGroups(int storeId = 0);
28+
IList<ForumGroup> GetAllForumGroups(int storeId = 0, bool showHidden = false);
2829

2930
/// <summary>
3031
/// Deletes a forum group
@@ -112,8 +113,9 @@ public partial interface IForumService
112113
/// </summary>
113114
/// <param name="forumId">The forum identifier</param>
114115
/// <param name="topicCount">Count of forum topics to return</param>
116+
/// <param name="showHidden">Whether to load hidden records</param>
115117
/// <returns>Forum Topics</returns>
116-
IList<ForumTopic> GetActiveTopics(int forumId, int topicCount);
118+
IList<ForumTopic> GetActiveTopics(int forumId, int topicCount, bool showHidden = false);
117119

118120
/// <summary>
119121
/// Deletes a forum topic

0 commit comments

Comments
 (0)