Added account initialization.

This commit is contained in:
Oceania2018 2018-08-02 22:54:23 -05:00
parent 33181e5db6
commit fe47fa2d5c
11 changed files with 132 additions and 18 deletions

View file

@ -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<IDbRecord>(() =>
{
_dc.Table<User>().Add(user);
});
$"Created user {user.Email}, user id: {user.Id}".Log(LogLevel.INFO);
}
public void Activate(string activationCode)
{
var activation = _dc.Table<UserAuth>().FirstOrDefault(x => x.ActivationCode == activationCode && !x.IsActivated);
if (activation == null)
{
}
else
{
_dc.Transaction<IDbRecord>(() =>
{
activation = _dc.Table<UserAuth>().FirstOrDefault(x => x.ActivationCode == activationCode);
activation.ActivationCode = String.Empty;
activation.IsActivated = true;
activation.UpdatedTime = DateTime.UtcNow;
});
}
}
}
}

View file

@ -1,8 +1,10 @@
using BotSharp.Core.Abstractions; using BotSharp.Core.Abstractions;
using EntityFrameworkCore.BootKit; using EntityFrameworkCore.BootKit;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
namespace BotSharp.Core.Accounts namespace BotSharp.Core.Accounts
@ -18,7 +20,20 @@ namespace BotSharp.Core.Accounts
private void ImportAccount(Database dc) 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<List<User>>(json);
users.ForEach(user =>
{
if (!dc.Table<User>().Any(x => x.UserName == user.UserName))
{
var core = new AccountCore(dc);
core.CreateUser(user);
core.Activate(user.Authenticaiton.ActivationCode);
}
});
} }
} }
} }

View file

@ -12,11 +12,11 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Haiping Chen</Authors> <Authors>Haiping Chen</Authors>
<Company /> <Company />
<Product>BotSharp Chabot Platform</Product> <Product>BotSharp Chatbot Platform</Product>
<Description>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.</Description> <Description>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.</Description>
<RepositoryType>MIT</RepositoryType> <RepositoryType>MIT</RepositoryType>
<RepositoryUrl>https://github.com/Oceania2018/BotSharp</RepositoryUrl> <RepositoryUrl>https://github.com/Oceania2018/BotSharp</RepositoryUrl>
<PackageTags>NLU, Chatbot, Bot, AI Bot</PackageTags> <PackageTags>NLU, Chatbot, Bot, AI Bot, Artificial Intelligence, RPA</PackageTags>
<Version>1.4.0</Version> <Version>1.4.0</Version>
<PackageReleaseNotes>Add NLP pipeline. Support training model by input context.</PackageReleaseNotes> <PackageReleaseNotes>Add NLP pipeline. Support training model by input context.</PackageReleaseNotes>
<Copyright>Since 2018 Haiping Chen</Copyright> <Copyright>Since 2018 Haiping Chen</Copyright>
@ -36,12 +36,13 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DotNetToolkit" Version="1.4.0" /> <PackageReference Include="DotNetToolkit" Version="1.4.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.8.0" />
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="RestSharp" Version="106.3.1" /> <PackageReference Include="RestSharp" Version="106.3.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\EntityFrameworkCore.BootKit\EntityFrameworkCore.BootKit\EntityFrameworkCore.BootKit.csproj" />
<ProjectReference Include="..\BotSharp.MachineLearning\BotSharp.MachineLearning.csproj" /> <ProjectReference Include="..\BotSharp.MachineLearning\BotSharp.MachineLearning.csproj" />
</ItemGroup> </ItemGroup>

View file

@ -45,7 +45,8 @@ namespace BotSharp.RestApi.Authentication
string hash = PasswordHelper.Hash(userModel.Password, user.Salt); string hash = PasswordHelper.Hash(userModel.Password, user.Salt);
if (user.Password == hash) 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 else
{ {

View file

@ -2,6 +2,19 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Haiping Chen</Authors>
<Company>Personal</Company>
<Product>BotSharp</Product>
<Description>Restful API for BotSharp.Core</Description>
<PackageProjectUrl>https://github.com/Oceania2018/BotSharp</PackageProjectUrl>
<RepositoryUrl>https://github.com/Oceania2018/BotSharp/tree/master/BotSharp.RestApi</RepositoryUrl>
<RepositoryType>MIT</RepositoryType>
<PackageIconUrl>https://raw.githubusercontent.com/Oceania2018/BotSharp/master/BotSharp.WebHost/wwwroot/images/BotSharp.png</PackageIconUrl>
<Copyright>Since 2018 Haiping Chen</Copyright>
<PackageLicenseUrl>https://github.com/Oceania2018/BotSharp/blob/master/LICENSE</PackageLicenseUrl>
<PackageTags>NLU, Chatbot, Bot, AI Bot</PackageTags>
<PackageReleaseNotes>Restful API for BotSharp.Core</PackageReleaseNotes>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View file

@ -3,6 +3,6 @@ import Env from './env';
let config = { let config = {
env: Env, env: Env,
baseURL: (Env == 'development' ? `http://localhost:3112` : `http://localhost:3112`), 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; export default config;

View file

@ -0,0 +1,12 @@
[
{
"userName": "support@botsharp.io",
"email": "support@botsharp.io",
"firstName": "Support",
"lastName": "Botsharp",
"description": "demo account",
"authenticaiton": {
"password": "botsharp"
}
}
]

View file

@ -57,7 +57,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="App_Data\DbInitializer\Agents\Rasa\" /> <Folder Include="App_Data\DbInitializer\Agents\Rasa\" />
<Folder Include="App_Data\DbInitializer\Accounts\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -0,0 +1,9 @@
{
"TokenAuthentication": {
"SecretKey": "lfo54FYneUCJNL2EjP9ZxQ==",
"Issuer": "BotSharp",
"Audience": "BotSharp",
"CookieName": "token",
"Subject": "BotSharp"
}
}

View file

@ -103,15 +103,21 @@ namespace BotSharp.WebHost
app.UseMvc(); app.UseMvc();
Database.Configuration = Configuration; AppDomain.CurrentDomain.SetData("DataPath", Path.Join(env.ContentRootPath, "App_Data"));
Database.ContentRootPath = env.ContentRootPath; AppDomain.CurrentDomain.SetData("Configuration", Configuration);
Database.Assemblies = Configuration.GetValue<String>("Assemblies").Split(','); AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath);
AppDomain.CurrentDomain.SetData("Assemblies", Configuration.GetValue<String>("Assemblies").Split(','));
Runcmd(); InitializationLoader loader = new InitializationLoader();
loader.Env = env;
loader.Config = Configuration;
loader.Load();
/*Runcmd();
var ai = new BotSharpAi(); var ai = new BotSharpAi();
ai.LoadAgent("6a9fd374-c43d-447a-97f2-f37540d0c725"); ai.LoadAgent("6a9fd374-c43d-447a-97f2-f37540d0c725");
ai.Train(); ai.Train();*/
} }
public void Runcmd () public void Runcmd ()

View file

@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.WebHost", "BotShar
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.MachineLearning", "BotSharp.MachineLearning\BotSharp.MachineLearning.csproj", "{E664115A-AE86-49E9-8AE4-D4589A568CD7}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.MachineLearning", "BotSharp.MachineLearning\BotSharp.MachineLearning.csproj", "{E664115A-AE86-49E9-8AE4-D4589A568CD7}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.BootKit", "..\EntityFrameworkCore.BootKit\EntityFrameworkCore.BootKit\EntityFrameworkCore.BootKit.csproj", "{A5F6D16F-E6F4-449E-AA52-7830B4897F88}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{E664115A-AE86-49E9-8AE4-D4589A568CD7}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE