From fe47fa2d5cccd448ee7692b9ecae1f031e489b71 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Thu, 2 Aug 2018 22:54:23 -0500 Subject: [PATCH] Added account initialization. --- BotSharp.Core/Accounts/AccountCore.cs | 64 +++++++++++++++++++ .../Accounts/AccountDbInitializer.cs | 17 ++++- BotSharp.Core/BotSharp.Core.csproj | 7 +- .../AuthenticationController.cs | 3 +- BotSharp.RestApi/BotSharp.RestApi.csproj | 13 ++++ BotSharp.UI/src/config/config.js | 2 +- .../DbInitializer/Accounts/users.json | 12 ++++ BotSharp.WebHost/BotSharp.WebHost.csproj | 1 - BotSharp.WebHost/Settings/auth.json | 9 +++ BotSharp.WebHost/Startup.cs | 16 +++-- BotSharp.sln | 6 -- 11 files changed, 132 insertions(+), 18 deletions(-) create mode 100644 BotSharp.Core/Accounts/AccountCore.cs create mode 100644 BotSharp.WebHost/App_Data/DbInitializer/Accounts/users.json create mode 100644 BotSharp.WebHost/Settings/auth.json diff --git a/BotSharp.Core/Accounts/AccountCore.cs b/BotSharp.Core/Accounts/AccountCore.cs new file mode 100644 index 00000000..8c048c6b --- /dev/null +++ b/BotSharp.Core/Accounts/AccountCore.cs @@ -0,0 +1,64 @@ +using DotNetToolkit; +using EntityFrameworkCore.BootKit; +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BotSharp.Core.Accounts +{ + public class AccountCore + { + private Database _dc; + private IConfiguration _config; + + public AccountCore(Database dc = null) + { + if (dc == null) + { + dc = new DefaultDataContextLoader().GetDefaultDc(); + } + else + { + _dc = dc; + } + + _config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration"); + } + + public void CreateUser(User user) + { + user.Authenticaiton.IsActivated = false; + user.Authenticaiton.Salt = PasswordHelper.GetSalt(); + user.Authenticaiton.Password = PasswordHelper.Hash(user.Authenticaiton.Password, user.Authenticaiton.Salt); + user.Authenticaiton.ActivationCode = Guid.NewGuid().ToString("N"); + + _dc.Transaction(() => + { + _dc.Table().Add(user); + }); + + + $"Created user {user.Email}, user id: {user.Id}".Log(LogLevel.INFO); + } + + public void Activate(string activationCode) + { + var activation = _dc.Table().FirstOrDefault(x => x.ActivationCode == activationCode && !x.IsActivated); + if (activation == null) + { + } + else + { + _dc.Transaction(() => + { + activation = _dc.Table().FirstOrDefault(x => x.ActivationCode == activationCode); + activation.ActivationCode = String.Empty; + activation.IsActivated = true; + activation.UpdatedTime = DateTime.UtcNow; + }); + } + } + } +} diff --git a/BotSharp.Core/Accounts/AccountDbInitializer.cs b/BotSharp.Core/Accounts/AccountDbInitializer.cs index abd95840..0e55e6e0 100644 --- a/BotSharp.Core/Accounts/AccountDbInitializer.cs +++ b/BotSharp.Core/Accounts/AccountDbInitializer.cs @@ -1,8 +1,10 @@ using BotSharp.Core.Abstractions; using EntityFrameworkCore.BootKit; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; namespace BotSharp.Core.Accounts @@ -18,7 +20,20 @@ namespace BotSharp.Core.Accounts private void ImportAccount(Database dc) { - var dataPath = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Accounts"); + var dataPath = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "DbInitializer", "Accounts"); + string json = File.ReadAllText(Path.Join(dataPath, "users.json")); + + var users = JsonConvert.DeserializeObject>(json); + users.ForEach(user => + { + if (!dc.Table().Any(x => x.UserName == user.UserName)) + { + var core = new AccountCore(dc); + core.CreateUser(user); + core.Activate(user.Authenticaiton.ActivationCode); + } + }); + } } } diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj index 32d95246..6378b2af 100644 --- a/BotSharp.Core/BotSharp.Core.csproj +++ b/BotSharp.Core/BotSharp.Core.csproj @@ -12,11 +12,11 @@ true Haiping Chen - BotSharp Chabot Platform + BotSharp Chatbot Platform Open source chatbot platform which is written in C# runs on .Net Core and is enterprise oriented. Integrated with multiple bot engines besides BotSharp bot engine. Modulized pipeline design make NLP tasks plugin easily. Abstract platform and NLP task, migrate existed chatbot from a platform into another platform perfectly through dump and restore. MIT https://github.com/Oceania2018/BotSharp - NLU, Chatbot, Bot, AI Bot + NLU, Chatbot, Bot, AI Bot, Artificial Intelligence, RPA 1.4.0 Add NLP pipeline. Support training model by input context. Since 2018 Haiping Chen @@ -36,12 +36,13 @@ + + - diff --git a/BotSharp.RestApi/Authentication/AuthenticationController.cs b/BotSharp.RestApi/Authentication/AuthenticationController.cs index d50135a9..72df1704 100644 --- a/BotSharp.RestApi/Authentication/AuthenticationController.cs +++ b/BotSharp.RestApi/Authentication/AuthenticationController.cs @@ -45,7 +45,8 @@ namespace BotSharp.RestApi.Authentication string hash = PasswordHelper.Hash(userModel.Password, user.Salt); if (user.Password == hash) { - return Ok(JwtToken.GenerateToken((IConfiguration)AppDomain.CurrentDomain.GetData("Configuration"), user.UserId)); + var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration"); + return Ok(JwtToken.GenerateToken(config, user.UserId)); } else { diff --git a/BotSharp.RestApi/BotSharp.RestApi.csproj b/BotSharp.RestApi/BotSharp.RestApi.csproj index ed3204a0..6c85eac9 100644 --- a/BotSharp.RestApi/BotSharp.RestApi.csproj +++ b/BotSharp.RestApi/BotSharp.RestApi.csproj @@ -2,6 +2,19 @@ netcoreapp2.1 + true + Haiping Chen + Personal + BotSharp + Restful API for BotSharp.Core + https://github.com/Oceania2018/BotSharp + https://github.com/Oceania2018/BotSharp/tree/master/BotSharp.RestApi + MIT + https://raw.githubusercontent.com/Oceania2018/BotSharp/master/BotSharp.WebHost/wwwroot/images/BotSharp.png + Since 2018 Haiping Chen + https://github.com/Oceania2018/BotSharp/blob/master/LICENSE + NLU, Chatbot, Bot, AI Bot + Restful API for BotSharp.Core diff --git a/BotSharp.UI/src/config/config.js b/BotSharp.UI/src/config/config.js index 2504523b..9453ea59 100644 --- a/BotSharp.UI/src/config/config.js +++ b/BotSharp.UI/src/config/config.js @@ -3,6 +3,6 @@ import Env from './env'; let config = { env: Env, baseURL: (Env == 'development' ? `http://localhost:3112` : `http://localhost:3112`), - testAccount: {username: `botsharp@gmail.com`, password: (Env == 'development' ? `botsharp` : `botsharp`)} + testAccount: {username: `support@botsharp.io`, password: (Env == 'development' ? `botsharp` : ``)} }; export default config; \ No newline at end of file diff --git a/BotSharp.WebHost/App_Data/DbInitializer/Accounts/users.json b/BotSharp.WebHost/App_Data/DbInitializer/Accounts/users.json new file mode 100644 index 00000000..d723a700 --- /dev/null +++ b/BotSharp.WebHost/App_Data/DbInitializer/Accounts/users.json @@ -0,0 +1,12 @@ +[ + { + "userName": "support@botsharp.io", + "email": "support@botsharp.io", + "firstName": "Support", + "lastName": "Botsharp", + "description": "demo account", + "authenticaiton": { + "password": "botsharp" + } + } +] diff --git a/BotSharp.WebHost/BotSharp.WebHost.csproj b/BotSharp.WebHost/BotSharp.WebHost.csproj index d029ebba..458f02fd 100644 --- a/BotSharp.WebHost/BotSharp.WebHost.csproj +++ b/BotSharp.WebHost/BotSharp.WebHost.csproj @@ -57,7 +57,6 @@ - diff --git a/BotSharp.WebHost/Settings/auth.json b/BotSharp.WebHost/Settings/auth.json new file mode 100644 index 00000000..a73f7f4a --- /dev/null +++ b/BotSharp.WebHost/Settings/auth.json @@ -0,0 +1,9 @@ +{ + "TokenAuthentication": { + "SecretKey": "lfo54FYneUCJNL2EjP9ZxQ==", + "Issuer": "BotSharp", + "Audience": "BotSharp", + "CookieName": "token", + "Subject": "BotSharp" + } +} diff --git a/BotSharp.WebHost/Startup.cs b/BotSharp.WebHost/Startup.cs index 0480512d..8c3f91d2 100644 --- a/BotSharp.WebHost/Startup.cs +++ b/BotSharp.WebHost/Startup.cs @@ -103,15 +103,21 @@ namespace BotSharp.WebHost app.UseMvc(); - Database.Configuration = Configuration; - Database.ContentRootPath = env.ContentRootPath; - Database.Assemblies = Configuration.GetValue("Assemblies").Split(','); + AppDomain.CurrentDomain.SetData("DataPath", Path.Join(env.ContentRootPath, "App_Data")); + AppDomain.CurrentDomain.SetData("Configuration", Configuration); + AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath); + AppDomain.CurrentDomain.SetData("Assemblies", Configuration.GetValue("Assemblies").Split(',')); - Runcmd(); + InitializationLoader loader = new InitializationLoader(); + loader.Env = env; + loader.Config = Configuration; + loader.Load(); + + /*Runcmd(); var ai = new BotSharpAi(); ai.LoadAgent("6a9fd374-c43d-447a-97f2-f37540d0c725"); - ai.Train(); + ai.Train();*/ } public void Runcmd () diff --git a/BotSharp.sln b/BotSharp.sln index d16f478e..c3bb81a6 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.WebHost", "BotShar EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.MachineLearning", "BotSharp.MachineLearning\BotSharp.MachineLearning.csproj", "{E664115A-AE86-49E9-8AE4-D4589A568CD7}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.BootKit", "..\EntityFrameworkCore.BootKit\EntityFrameworkCore.BootKit\EntityFrameworkCore.BootKit.csproj", "{A5F6D16F-E6F4-449E-AA52-7830B4897F88}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,10 +39,6 @@ Global {E664115A-AE86-49E9-8AE4-D4589A568CD7}.Debug|Any CPU.Build.0 = Debug|Any CPU {E664115A-AE86-49E9-8AE4-D4589A568CD7}.Release|Any CPU.ActiveCfg = Release|Any CPU {E664115A-AE86-49E9-8AE4-D4589A568CD7}.Release|Any CPU.Build.0 = Release|Any CPU - {A5F6D16F-E6F4-449E-AA52-7830B4897F88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A5F6D16F-E6F4-449E-AA52-7830B4897F88}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A5F6D16F-E6F4-449E-AA52-7830B4897F88}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A5F6D16F-E6F4-449E-AA52-7830B4897F88}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE