Skip to content

Commit

Permalink
Merge pull request #94 from alirizaadiyahsi/dev
Browse files Browse the repository at this point in the history
v0.2.0 release
  • Loading branch information
alirizaadiyahsi authored Nov 27, 2018
2 parents aff5b34 + 9e4741a commit 28721f2
Show file tree
Hide file tree
Showing 33 changed files with 232 additions and 137 deletions.
4 changes: 1 addition & 3 deletions Nucleus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{51A63C9A-C
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{CBE903BE-AD0D-4857-98E4-CCA1AD73902E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "doc", "doc", "{47DBDDFE-C870-4057-8312-219461BA268C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nucleus.Application", "src\Nucleus.Application\Nucleus.Application.csproj", "{788EBFEF-7A50-4AB7-807A-4147D49E237E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nucleus.EntityFramework", "src\Nucleus.EntityFramework\Nucleus.EntityFramework.csproj", "{73195EBE-BDE4-428C-865D-E7F589F3DD52}"
Expand All @@ -27,7 +25,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nucleus.Tests.Application",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nucleus.Tests.Web.Api", "test\Nucleus.Tests.Web.Api\Nucleus.Tests.Web.Api.csproj", "{E36E92AC-E12D-4B70-A355-6CBC1AFDAB43}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nucleus.Web.Vue", "src\Nucleus.Web.Vue\Nucleus.Web.Vue.csproj", "{3979C24B-DFE1-4090-9743-8DFA9C52878E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nucleus.Web.Vue", "src\Nucleus.Web.Vue\Nucleus.Web.Vue.csproj", "{3979C24B-DFE1-4090-9743-8DFA9C52878E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<img align="left" src="_images/Nucleus.png" alt="Nucleus" class="img-thumbnail" width="144" />

# Nucleus

Web API startup template with a Vue Client demo.

<br/>
<br/>

## How to Start?

- Select `Nucleus.Web.Api` project "**Set as Startup Project**"
Expand Down Expand Up @@ -40,6 +45,63 @@ Web API startup template with a Vue Client demo.

<img src="_images/swagger-ui.png" alt="Swagger UI" class="img-thumbnail" />

# Document

## Adding New Language

- Add json file to store language keys and values to `Nucleus\src\Nucleus.Web.Vue\src\assets\js\locales\tr.json`
- Copy `en.json` content and translate the values to target language.
- Add country flag to `Nucleus.Web.Vue\src\assets\images\icons\flags\tr.png`. Get images from http://www.iconarchive.com/show/flag-icons-by-gosquared.html
- Add language to language selection menu in `Nucleus\src\Nucleus.Web.Vue\src\account\account-layout.vue` and `Nucleus\src\Nucleus.Web.Vue\src\admin\components\menu\top-menu\top-menu.vue` like following

**account-layout.vue**

````html
<v-layout align-center justify-center row fill-height>
<v-menu class="mt-3">
<v-btn slot="activator" color="primary" dark outline round>
<img :src="require('@/assets/images/icons/flags/' + selectedLanguage.languageCode + '.png')" class="mr-2 ml-1" />
{{selectedLanguage.languageName}}
<v-icon dark class="ml-3">arrow_drop_down</v-icon>
</v-btn>
<v-list>
<v-list-tile @click="changeLanguage('en', 'English')">
<img src="@/assets/images/icons/flags/en.png" class="mr-2" />
<v-list-tile-title>English</v-list-tile-title>
</v-list-tile>
<v-list-tile @click="changeLanguage('tr', 'Türkçe')">
<img src="@/assets/images/icons/flags/tr.png" class="mr-2" />
<v-list-tile-title>Türkçe</v-list-tile-title>
</v-list-tile>
</v-list>
</v-menu>
</v-layout>
````

**top-menu.vue**

````html
<v-menu>
<v-btn slot="activator" color="primary">
<img :src="require('@/assets/images/icons/flags/' + selectedLanguage.languageCode + '.png')" class="mr-2" />
{{selectedLanguage.languageName}}
<v-icon dark class="ml-3">arrow_drop_down</v-icon>
</v-btn>
<v-list>
<v-list-tile @click="changeLanguage('en', 'English')">
<img src="@/assets/images/icons/flags/en.png" class="mr-2" />
<v-list-tile-title>English</v-list-tile-title>
</v-list-tile>
<v-list-tile @click="changeLanguage('tr', 'Türkçe')">
<img src="@/assets/images/icons/flags/tr.png" class="mr-2" />
<v-list-tile-title>Türkçe</v-list-tile-title>
</v-list-tile>
</v-list>
</v-menu>
````

###

### Tags & Technologies

- [ASP.NET Core Web API](https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.1)
Expand Down
Binary file added _images/Nucleus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Nucleus.Application/Account/Dto/ChangePasswordInput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nucleus.Application.Users.Dto
namespace Nucleus.Application.Account.Dto
{
public class ChangePasswordInput
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nucleus.Application/Account/Dto/LoginInput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nucleus.Application.Users.Dto
namespace Nucleus.Application.Account.Dto
{
public class LoginInput
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nucleus.Application/Account/Dto/LoginOutput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nucleus.Application.Users.Dto
namespace Nucleus.Application.Account.Dto
{
public class LoginOutput
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nucleus.Application/Account/Dto/RegisterInput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Nucleus.Application.Users.Dto
namespace Nucleus.Application.Account.Dto
{
public class RegisterInput
{
Expand Down
3 changes: 3 additions & 0 deletions src/Nucleus.Application/Permissions/IPermissionAppService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Nucleus.Application.Permissions.Dto;
using Nucleus.Core.Permissions;

namespace Nucleus.Application.Permissions
{
Expand All @@ -9,5 +10,7 @@ public interface IPermissionAppService
Task<IEnumerable<PermissionDto>> GetGrantedPermissionsAsync(string userNameOrEmail);

Task<bool> IsUserGrantedToPermissionAsync(string userNameOrEmail, string permissionName);

void InitializePermissions(List<Permission> permissions);
}
}
44 changes: 33 additions & 11 deletions src/Nucleus.Application/Permissions/PermissionAppService.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Nucleus.Application.Permissions.Dto;
using Nucleus.Core.Permissions;
using Nucleus.Core.Roles;
using Nucleus.EntityFramework;
using Nucleus.Utilities.Collections;
using Nucleus.Utilities.Extensions.Collections;
using Nucleus.Utilities.Extensions.PrimitiveTypes;
using System.Linq.Dynamic.Core;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Nucleus.Core.Permissions;
using Nucleus.Core.Roles;
using Nucleus.Core.Users;
using Nucleus.EntityFramework;

namespace Nucleus.Application.Permissions
{
public class PermissionAppService : IPermissionAppService
{
private readonly UserManager<User> _userManager;
private readonly IMapper _mapper;
private readonly NucleusDbContext _dbContext;

public PermissionAppService(
UserManager<User> userManager,
IMapper mapper
)
IMapper mapper,
NucleusDbContext dbContext)
{
_userManager = userManager;
_mapper = mapper;
_dbContext = dbContext;
}

public async Task<IEnumerable<PermissionDto>> GetGrantedPermissionsAsync(string userNameOrEmail)
Expand Down Expand Up @@ -60,5 +57,30 @@ public async Task<bool> IsUserGrantedToPermissionAsync(string userNameOrEmail, s

return grantedPermissions.Any(p => p.Name == permissionName);
}

public void InitializePermissions(List<Permission> permissions)
{
_dbContext.RolePermissions.RemoveRange(_dbContext.RolePermissions.Where(rp => rp.RoleId == DefaultRoles.Admin.Id));
_dbContext.SaveChanges();

_dbContext.Permissions.RemoveRange(_dbContext.Permissions);
_dbContext.SaveChanges();

_dbContext.AddRange(permissions);
GrantAllPermissionsToAdminRole(permissions);
_dbContext.SaveChanges();
}

private void GrantAllPermissionsToAdminRole(List<Permission> permissions)
{
foreach (var permission in permissions)
{
_dbContext.RolePermissions.Add(new RolePermission
{
PermissionId = permission.Id,
RoleId = DefaultRoles.Admin.Id
});
}
}
}
}
3 changes: 2 additions & 1 deletion src/Nucleus.Application/Roles/RoleAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public async Task<IdentityResult> EditRoleAsync(CreateOrUpdateRoleInput input)
}
role.Name = input.Role.Name;
role.RolePermissions.Clear();

var updateRoleResult = await _roleManager.UpdateAsync(role);
if (updateRoleResult.Succeeded)
{
Expand All @@ -101,7 +102,6 @@ public async Task<IdentityResult> EditRoleAsync(CreateOrUpdateRoleInput input)
public async Task<IdentityResult> RemoveRoleAsync(Guid id)
{
var role = _roleManager.Roles.FirstOrDefault(r => r.Id == id);

if (role == null)
{
return IdentityResult.Failed(new IdentityError()
Expand All @@ -128,6 +128,7 @@ public async Task<IdentityResult> RemoveRoleAsync(Guid id)

role.RolePermissions.Clear();
role.UserRoles.Clear();

return removeRoleResult;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Nucleus.Application/Users/UserAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public async Task<IdentityResult> EditUserAsync(CreateOrUpdateUserInput input)
public async Task<IdentityResult> RemoveUserAsync(Guid id)
{
var user = _userManager.Users.FirstOrDefault(u => u.Id == id);

if (user == null)
{
return IdentityResult.Failed(new IdentityError
Expand All @@ -133,6 +132,7 @@ public async Task<IdentityResult> RemoveUserAsync(Guid id)
}

user.UserRoles.Clear();

return removeUserResult;
}

Expand Down
4 changes: 0 additions & 4 deletions src/Nucleus.EntityFramework/NucleusDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
b.ToTable("UserRole");

b.HasOne(ur => ur.User)
.WithMany(u => u.UserRoles)
.HasForeignKey(ur => ur.UserId);

b.HasOne(ur => ur.User)
.WithMany(u => u.UserRoles)
.HasForeignKey(ur => ur.UserId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Nucleus.Application.Account.Dto;
using Nucleus.Application.Dto;
using Nucleus.Application.Permissions;
using Nucleus.Application.Permissions.Dto;
Expand Down
14 changes: 13 additions & 1 deletion src/Nucleus.Web.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Nucleus.Application.Permissions;
using Nucleus.Core.Permissions;
using Serilog;
using Serilog.Events;

Expand All @@ -26,7 +29,16 @@ public static int Main(string[] args)
try
{
Log.Information("Starting web host");
BuildWebHost(args).Run();

Log.Information("Starting web host");
var host = BuildWebHost(args);
using (var scope = host.Services.CreateScope())
{
var permissionAppService = scope.ServiceProvider.GetRequiredService<IPermissionAppService>();
permissionAppService.InitializePermissions(DefaultPermissions.All());
}

host.Run();
return 0;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Nucleus.Web.Core.Authentication
{
public static class JwtTokenAuthConfigurer
public static class JwtTokenAuthConfigure
{
private static SymmetricSecurityKey _signingKey;
private static JwtTokenConfiguration _jwtTokenConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void ConfigureAuthentication(this IServiceCollection services, ICo
.AddEntityFrameworkStores<NucleusDbContext>()
.AddDefaultTokenProviders();

JwtTokenAuthConfigurer.Configure(services, configuration);
JwtTokenAuthConfigure.Configure(services, configuration);

services.AddAuthorization(options =>
{
Expand Down
7 changes: 5 additions & 2 deletions src/Nucleus.Web.Vue/Nucleus.Web.Vue.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
</PropertyGroup>

<ItemGroup>
<Content Remove="compilerconfig.json" />
<Compile Remove="wwwroot\**" />
<Content Remove="wwwroot\**" />
<EmbeddedResource Remove="wwwroot\**" />
<None Remove="wwwroot\**" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
<Content Remove="compilerconfig.json" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 28721f2

Please sign in to comment.