Ejemplo n.º 1
0
        private async Task CreateUser(IrcPrivMessage request, string nickname, string email, string ircHostname,
                                      string matchType, string role, CancellationToken cancellationToken)
        {
            // If the user already exists, bail.
            if (null != await _userManager.FindByEmailAsync(email))
            {
                await SendResponse(request, $"{email} already exists, bailing...");
            }
            else
            {
                bool.TryParse(matchType, out var prefixMatch);

                // Create our user. Apparently the pre-built identity login methods prefer the username and email to be equal.
                var user = new SpikeCoreUser {
                    UserName = email, Email = email
                };
                await _userManager.CreateAsync(user);

                // Associate the proper roles with the user.
                var persistedUser = await _userManager.FindByEmailAsync(email);

                var roles = role.Equals("op", StringComparison.InvariantCultureIgnoreCase) ? OpRoles : RegularRoles;
                await _userManager.AddToRolesAsync(persistedUser, roles);

                // Associate a login with the user, so they can use the bot.
                await _userManager.AddLoginAsync(persistedUser,
                                                 new UserLoginInfo(IrcLoginProvider, ircHostname, nickname));

                // Prefix match is a custom field, so we'll update it out of band via EF directly.
                if (prefixMatch)
                {
                    var login = _context.UserLogins.Single(record =>
                                                           record.LoginProvider == IrcLoginProvider && record.ProviderKey == ircHostname);

                    login.MatchType = "StartsWith";

                    _context.UserLogins.Update(login);
                    await _context.SaveChangesAsync(cancellationToken);
                }

                Log.Information("{IrcUserName} (identity: {IdentityUserName}) has just created user {AffectedUserName}", request.UserName,
                                request.IdentityUser.UserName, email);
                await SendResponse(request,
                                   $"successfully created user {nickname} (email: {email}), with roles [{string.Join(", ", roles)}] (match type: {(prefixMatch ? "StartsWith" : "Literal")})");
            }
        }
Ejemplo n.º 2
0
 protected override bool AccessAllowed(SpikeCoreUser user)
 {
     return(base.AccessAllowed(user) && user.IsAdmin());
 }
Ejemplo n.º 3
0
 protected virtual bool AccessAllowed(SpikeCoreUser user)
 {
     // By default a command can be run by any known user with at least one role.
     return(null != user && user.Roles.Any());
 }