Skip to content

Commit fe47fa2

Browse files
committed
Added account initialization.
1 parent 33181e5 commit fe47fa2

11 files changed

Lines changed: 132 additions & 18 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using DotNetToolkit;
2+
using EntityFrameworkCore.BootKit;
3+
using Microsoft.Extensions.Configuration;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
9+
namespace BotSharp.Core.Accounts
10+
{
11+
public class AccountCore
12+
{
13+
private Database _dc;
14+
private IConfiguration _config;
15+
16+
public AccountCore(Database dc = null)
17+
{
18+
if (dc == null)
19+
{
20+
dc = new DefaultDataContextLoader().GetDefaultDc();
21+
}
22+
else
23+
{
24+
_dc = dc;
25+
}
26+
27+
_config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
28+
}
29+
30+
public void CreateUser(User user)
31+
{
32+
user.Authenticaiton.IsActivated = false;
33+
user.Authenticaiton.Salt = PasswordHelper.GetSalt();
34+
user.Authenticaiton.Password = PasswordHelper.Hash(user.Authenticaiton.Password, user.Authenticaiton.Salt);
35+
user.Authenticaiton.ActivationCode = Guid.NewGuid().ToString("N");
36+
37+
_dc.Transaction<IDbRecord>(() =>
38+
{
39+
_dc.Table<User>().Add(user);
40+
});
41+
42+
43+
$"Created user {user.Email}, user id: {user.Id}".Log(LogLevel.INFO);
44+
}
45+
46+
public void Activate(string activationCode)
47+
{
48+
var activation = _dc.Table<UserAuth>().FirstOrDefault(x => x.ActivationCode == activationCode && !x.IsActivated);
49+
if (activation == null)
50+
{
51+
}
52+
else
53+
{
54+
_dc.Transaction<IDbRecord>(() =>
55+
{
56+
activation = _dc.Table<UserAuth>().FirstOrDefault(x => x.ActivationCode == activationCode);
57+
activation.ActivationCode = String.Empty;
58+
activation.IsActivated = true;
59+
activation.UpdatedTime = DateTime.UtcNow;
60+
});
61+
}
62+
}
63+
}
64+
}

BotSharp.Core/Accounts/AccountDbInitializer.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using BotSharp.Core.Abstractions;
22
using EntityFrameworkCore.BootKit;
3+
using Newtonsoft.Json;
34
using System;
45
using System.Collections.Generic;
56
using System.IO;
7+
using System.Linq;
68
using System.Text;
79

810
namespace BotSharp.Core.Accounts
@@ -18,7 +20,20 @@ public void Load(Database dc)
1820

1921
private void ImportAccount(Database dc)
2022
{
21-
var dataPath = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Accounts");
23+
var dataPath = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "DbInitializer", "Accounts");
24+
string json = File.ReadAllText(Path.Join(dataPath, "users.json"));
25+
26+
var users = JsonConvert.DeserializeObject<List<User>>(json);
27+
users.ForEach(user =>
28+
{
29+
if (!dc.Table<User>().Any(x => x.UserName == user.UserName))
30+
{
31+
var core = new AccountCore(dc);
32+
core.CreateUser(user);
33+
core.Activate(user.Authenticaiton.ActivationCode);
34+
}
35+
});
36+
2237
}
2338
}
2439
}

BotSharp.Core/BotSharp.Core.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1313
<Authors>Haiping Chen</Authors>
1414
<Company />
15-
<Product>BotSharp Chabot Platform</Product>
15+
<Product>BotSharp Chatbot Platform</Product>
1616
<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>
1717
<RepositoryType>MIT</RepositoryType>
1818
<RepositoryUrl>https://github.com/Oceania2018/BotSharp</RepositoryUrl>
19-
<PackageTags>NLU, Chatbot, Bot, AI Bot</PackageTags>
19+
<PackageTags>NLU, Chatbot, Bot, AI Bot, Artificial Intelligence, RPA</PackageTags>
2020
<Version>1.4.0</Version>
2121
<PackageReleaseNotes>Add NLP pipeline. Support training model by input context.</PackageReleaseNotes>
2222
<Copyright>Since 2018 Haiping Chen</Copyright>
@@ -36,12 +36,13 @@
3636

3737
<ItemGroup>
3838
<PackageReference Include="DotNetToolkit" Version="1.4.0" />
39+
<PackageReference Include="EntityFrameworkCore.BootKit" Version="1.8.0" />
40+
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="2.1.1" />
3941
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
4042
<PackageReference Include="RestSharp" Version="106.3.1" />
4143
</ItemGroup>
4244

4345
<ItemGroup>
44-
<ProjectReference Include="..\..\EntityFrameworkCore.BootKit\EntityFrameworkCore.BootKit\EntityFrameworkCore.BootKit.csproj" />
4546
<ProjectReference Include="..\BotSharp.MachineLearning\BotSharp.MachineLearning.csproj" />
4647
</ItemGroup>
4748

BotSharp.RestApi/Authentication/AuthenticationController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ join auth in dc.Table<UserAuth>() on usr.Id equals auth.UserId
4545
string hash = PasswordHelper.Hash(userModel.Password, user.Salt);
4646
if (user.Password == hash)
4747
{
48-
return Ok(JwtToken.GenerateToken((IConfiguration)AppDomain.CurrentDomain.GetData("Configuration"), user.UserId));
48+
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
49+
return Ok(JwtToken.GenerateToken(config, user.UserId));
4950
}
5051
else
5152
{

BotSharp.RestApi/BotSharp.RestApi.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<Authors>Haiping Chen</Authors>
7+
<Company>Personal</Company>
8+
<Product>BotSharp</Product>
9+
<Description>Restful API for BotSharp.Core</Description>
10+
<PackageProjectUrl>https://github.com/Oceania2018/BotSharp</PackageProjectUrl>
11+
<RepositoryUrl>https://github.com/Oceania2018/BotSharp/tree/master/BotSharp.RestApi</RepositoryUrl>
12+
<RepositoryType>MIT</RepositoryType>
13+
<PackageIconUrl>https://raw.githubusercontent.com/Oceania2018/BotSharp/master/BotSharp.WebHost/wwwroot/images/BotSharp.png</PackageIconUrl>
14+
<Copyright>Since 2018 Haiping Chen</Copyright>
15+
<PackageLicenseUrl>https://github.com/Oceania2018/BotSharp/blob/master/LICENSE</PackageLicenseUrl>
16+
<PackageTags>NLU, Chatbot, Bot, AI Bot</PackageTags>
17+
<PackageReleaseNotes>Restful API for BotSharp.Core</PackageReleaseNotes>
518
</PropertyGroup>
619

720
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

BotSharp.UI/src/config/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import Env from './env';
33
let config = {
44
env: Env,
55
baseURL: (Env == 'development' ? `http://localhost:3112` : `http://localhost:3112`),
6-
testAccount: {username: `botsharp@gmail.com`, password: (Env == 'development' ? `botsharp` : `botsharp`)}
6+
testAccount: {username: `support@botsharp.io`, password: (Env == 'development' ? `botsharp` : ``)}
77
};
88
export default config;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"userName": "support@botsharp.io",
4+
"email": "support@botsharp.io",
5+
"firstName": "Support",
6+
"lastName": "Botsharp",
7+
"description": "demo account",
8+
"authenticaiton": {
9+
"password": "botsharp"
10+
}
11+
}
12+
]

BotSharp.WebHost/BotSharp.WebHost.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757

5858
<ItemGroup>
5959
<Folder Include="App_Data\DbInitializer\Agents\Rasa\" />
60-
<Folder Include="App_Data\DbInitializer\Accounts\" />
6160
</ItemGroup>
6261

6362
<ItemGroup>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"TokenAuthentication": {
3+
"SecretKey": "lfo54FYneUCJNL2EjP9ZxQ==",
4+
"Issuer": "BotSharp",
5+
"Audience": "BotSharp",
6+
"CookieName": "token",
7+
"Subject": "BotSharp"
8+
}
9+
}

BotSharp.WebHost/Startup.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,21 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
103103

104104
app.UseMvc();
105105

106-
Database.Configuration = Configuration;
107-
Database.ContentRootPath = env.ContentRootPath;
108-
Database.Assemblies = Configuration.GetValue<String>("Assemblies").Split(',');
106+
AppDomain.CurrentDomain.SetData("DataPath", Path.Join(env.ContentRootPath, "App_Data"));
107+
AppDomain.CurrentDomain.SetData("Configuration", Configuration);
108+
AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath);
109+
AppDomain.CurrentDomain.SetData("Assemblies", Configuration.GetValue<String>("Assemblies").Split(','));
109110

110-
Runcmd();
111+
InitializationLoader loader = new InitializationLoader();
112+
loader.Env = env;
113+
loader.Config = Configuration;
114+
loader.Load();
115+
116+
/*Runcmd();
111117
112118
var ai = new BotSharpAi();
113119
ai.LoadAgent("6a9fd374-c43d-447a-97f2-f37540d0c725");
114-
ai.Train();
120+
ai.Train();*/
115121
}
116122

117123
public void Runcmd ()

0 commit comments

Comments
 (0)