Skip to content

Commit d6819f3

Browse files
committed
resolved #56
1 parent e5824e5 commit d6819f3

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

src/AbpCompanyName.AbpProjectName.Application/Sessions/SessionAppService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
using System.Threading.Tasks;
22
using Abp.Auditing;
3-
using Abp.Authorization;
43
using Abp.AutoMapper;
54
using AbpCompanyName.AbpProjectName.Sessions.Dto;
65

76
namespace AbpCompanyName.AbpProjectName.Sessions
87
{
9-
[AbpAuthorize]
108
public class SessionAppService : AbpProjectNameAppServiceBase, ISessionAppService
119
{
1210
[DisableAuditing]
1311
public async Task<GetCurrentLoginInformationsOutput> GetCurrentLoginInformations()
1412
{
15-
var output = new GetCurrentLoginInformationsOutput
13+
var output = new GetCurrentLoginInformationsOutput();
14+
15+
if (AbpSession.UserId.HasValue)
1616
{
17-
User = (await GetCurrentUserAsync()).MapTo<UserLoginInfoDto>()
18-
};
17+
output.User = (await GetCurrentUserAsync()).MapTo<UserLoginInfoDto>();
18+
}
1919

2020
if (AbpSession.TenantId.HasValue)
2121
{

src/AbpCompanyName.AbpProjectName.WebSpaAngular/App/Main/views/layout/header.cshtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@
2828
<li ng-repeat="language in vm.languages" ng-hide="vm.currentLanguage.name == language.name"><a href="@(ApplicationPath)AbpLocalization/ChangeCulture?cultureName={{language.name}}"><i ng-class="language.icon"></i> {{language.displayName}}</a></li>
2929
</ul>
3030
</li>
31-
<li class="dropdown">
31+
<li class="dropdown" ng-if="vm.isUserSignedIn()">
3232
<a href="" data-toggle="dropdown"><i class="fa fa-user"></i> <span>{{vm.getShownUserName()}}</span> <b class="caret"></b></a>
3333
<ul class="dropdown-menu">
3434
<li><a href="@Url.Action("Logout", "Account")"><i class="fa fa-sign-out"></i> @L("Logout")</a></li>
3535
</ul>
3636
</li>
37+
<li ng-if="!vm.isUserSignedIn()">
38+
<a href="@Url.Action("Login", "Account")"><i class="fa fa-sign-in"></i> @L("LogIn")</a>
39+
</li>
3740
</ul>
3841
</div>
3942
</div>

src/AbpCompanyName.AbpProjectName.WebSpaAngular/App/Main/views/layout/header.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
vm.menu = abp.nav.menus.MainMenu;
1212
vm.currentMenuName = $state.current.menu;
13+
vm.isUserSignedIn = function () { return !!appSession.user; };
1314

1415
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
1516
vm.currentMenuName = toState.menu;

src/AbpCompanyName.AbpProjectName.WebSpaAngular/App_Start/AbpProjectNameNavigationProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public override void SetNavigation(INavigationProviderContext context)
2020
"Home",
2121
new LocalizableString("HomePage", AbpProjectNameConsts.LocalizationSourceName),
2222
url: "#/",
23-
icon: "fa fa-home"
23+
icon: "fa fa-home",
24+
requiresAuthentication: true
2425
)
2526
).AddItem(
2627
new MenuItemDefinition(

src/Tests/AbpCompanyName.AbpProjectName.Tests/AbpProjectNameTestBase.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ protected void LoginAsDefaultTenantAdmin()
205205
LoginAsTenant(Tenant.DefaultTenantName, User.AdminUserName);
206206
}
207207

208+
protected void LogoutAsDefaultTenant()
209+
{
210+
LogoutAsTenant(Tenant.DefaultTenantName);
211+
}
212+
208213
protected void LoginAsHost(string userName)
209214
{
210215
Resolve<IMultiTenancyConfig>().IsEnabled = true;
@@ -223,6 +228,14 @@ protected void LoginAsHost(string userName)
223228
AbpSession.UserId = user.Id;
224229
}
225230

231+
protected void LogoutAsHost()
232+
{
233+
Resolve<IMultiTenancyConfig>().IsEnabled = true;
234+
235+
AbpSession.TenantId = null;
236+
AbpSession.UserId = null;
237+
}
238+
226239
protected void LoginAsTenant(string tenancyName, string userName)
227240
{
228241
var tenant = UsingDbContext(context => context.Tenants.FirstOrDefault(t => t.TenancyName == tenancyName));
@@ -245,8 +258,20 @@ protected void LoginAsTenant(string tenancyName, string userName)
245258
AbpSession.UserId = user.Id;
246259
}
247260

261+
protected void LogoutAsTenant(string tenancyName)
262+
{
263+
var tenant = UsingDbContext(context => context.Tenants.FirstOrDefault(t => t.TenancyName == tenancyName));
264+
if (tenant == null)
265+
{
266+
throw new Exception("There is no tenant: " + tenancyName);
267+
}
268+
269+
AbpSession.TenantId = tenant.Id;
270+
AbpSession.UserId = null;
271+
}
272+
248273
#endregion
249-
274+
250275
/// <summary>
251276
/// Gets current user if <see cref="IAbpSession.UserId"/> is not null.
252277
/// Throws exception if it's null.

src/Tests/AbpCompanyName.AbpProjectName.Tests/Sessions/SessionAppService_Tests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,37 @@ public async Task Should_Get_Current_User_And_Tenant_When_Logged_In_As_Tenant()
4848
output.Tenant.ShouldNotBe(null);
4949
output.Tenant.Name.ShouldBe(currentTenant.Name);
5050
}
51+
52+
[Fact]
53+
public async Task Should_Not_Get_User_When_Not_Logged_In_As_Host()
54+
{
55+
//Arrange
56+
LogoutAsHost();
57+
58+
//Act
59+
var output = await _sessionAppService.GetCurrentLoginInformations();
60+
61+
//Assert
62+
output.User.ShouldBeNull();
63+
output.Tenant.ShouldBeNull();
64+
}
65+
66+
[Fact]
67+
public async Task Should_Not_Get_User_When_Not_Logged_In_As_Tenant()
68+
{
69+
//Arrange
70+
LogoutAsDefaultTenant();
71+
72+
//Act
73+
var output = await _sessionAppService.GetCurrentLoginInformations();
74+
75+
//Assert
76+
var currentTenant = await GetCurrentTenantAsync();
77+
78+
output.User.ShouldBeNull();
79+
80+
output.Tenant.ShouldNotBeNull();
81+
output.Tenant.Name.ShouldBe(currentTenant.Name);
82+
}
5183
}
5284
}

0 commit comments

Comments
 (0)