-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathUserCommands.cs
55 lines (45 loc) · 1.66 KB
/
UserCommands.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using OrchardCore.Environment.Commands;
using OrchardCore.Users.Models;
using OrchardCore.Users.Services;
namespace OrchardCore.Users.Commands
{
public class UserCommands : DefaultCommandHandler
{
private readonly IUserService _userService;
public UserCommands(
IUserService userService,
IStringLocalizer<UserCommands> localizer) : base(localizer)
{
_userService = userService;
}
[OrchardSwitch]
public string UserName { get; set; }
[OrchardSwitch]
public string Password { get; set; }
[OrchardSwitch]
public string Email { get; set; }
[OrchardSwitch]
public string Roles { get; set; }
[CommandName("createUser")]
[CommandHelp("createUser /UserName:<username> /Password:<password> /Email:<email> /Roles:{rolename,rolename,...}\r\n\t" + "Creates a new User")]
[OrchardSwitches("UserName,Password,Email,Roles")]
public async Task CreateUserAsync()
{
var roleNames = (Roles ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries).ToArray();
var valid = true;
await _userService.CreateUserAsync(new User { UserName = UserName, Email = Email, RoleNames = roleNames, EmailConfirmed = true }, Password, (key, message) =>
{
valid = false;
Context.Output.WriteLine(message);
});
if (valid)
{
Context.Output.WriteLine(S["User created successfully"]);
}
}
}
}