Beispiel #1
0
        /// <summary>
        /// Add a user to a server's database
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <bool> AddUser(IGuildUser user)
        {
            try
            {
                using (var db = new UserContext()) {
                    var u = await db.Users.AsAsyncEnumerable().Where(x => x.GuildId == user.GuildId.ToString() && x.DiscordId == user.Id.ToString()).FirstOrDefaultAsync();

                    if (u == null)
                    {
                        var newUser = new User();
                        newUser.DiscordId = user.Id.ToString();
                        newUser.GuildId   = user.GuildId.ToString();
                        newUser.Points    = 0;
                        newUser.RankLevel = 0;
                        newUser.PermLevel = 0;
                        await db.AddAsync(newUser);

                        await db.SaveChangesAsync();

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                Connection.Close();
                Console.WriteLine("Error adding user");
                Console.WriteLine(e);
                return(false);
            }
        }
Beispiel #2
0
        public async Task <ActionResult <Group> > PostGroup(Group group)
        {
            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity("Must include name"));
            }
            try
            {
                string userId = User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
                group.OwnerId = userId;
                await context.AddAsync(group);

                await context.SaveChangesAsync();

                return(Ok(context.Groups.Include(g => g.Owner).Include(g => g.Issues).First(g => g.Name == group.Name)));
            }
            catch (Exception err)
            {
                if (err.Source == "Microsoft.EntityFrameworkCore.Relational")
                {
                    return(Conflict($"Group already exists with name '{group.Name}'"));
                }
                return(StatusCode(500, err));
            }
        }
Beispiel #3
0
        public async Task Register(string username, string password)
        {
            _iLogger.LogInformation("AuthRepository.Register called {Username}", username);

            var passwordSalt = _passwordHasher.GetSalt();

            var loginModel = new LoginModel()
            {
                UserName     = username,
                Password     = _passwordHasher.GetHashedSaltedPassword(password, passwordSalt),
                PasswordSalt = passwordSalt
            };

            if (loginModel == null)
            {
                _iLogger.LogWarning("Error creating new user", username);
            }

            await _userContext.AddAsync(loginModel);

            _iLogger.LogInformation("Adding user to db", username);
            await _userContext.SaveChangesAsync();

            _iLogger.LogInformation("Added user to db", username);
        }
Beispiel #4
0
        public async Task <User> Register(string email, string login, string password, string confirmPassword)
        {
            if (password != confirmPassword)
            {
                throw new ConfirmPasswordException();
            }


            if (context.Users.FirstOrDefault(x => x.Login == login) != null)
            {
                throw new LoginAlreadyExistsException(login);
            }

            if (context.Users.FirstOrDefault(x => x.Email == email) != null)
            {
                throw new EmailAlreadyExistsException(email);
            }

            string hashedPassword = hasher.HashPassword(password);
            User   user           = new User()
            {
                Login    = login,
                Email    = email,
                Passwd   = hashedPassword,
                Username = login
            };



            await context.AddAsync(user);

            await context.SaveChangesAsync();

            return(user);
        }
Beispiel #5
0
        public async Task <bool> AddUser(Services.User user)
        {
            bool Result = false;
            await Context.AddAsync(user);

            Result = await Context.SaveChangesAsync() > 0;

            return(Result);
        }
Beispiel #6
0
        public async Task <User> Create(string username, string email, string password)
        {
            var encryptedPassword = passwordEncrypt.HashReturnSalt(password);
            var user = new User()
            {
                Username = username, Email = email, Salt = encryptedPassword.Item1, Password = encryptedPassword.Item2
            };

            await context.AddAsync(user);

            return(await context.SaveChangesAsync() > 0 ? user : null);
        }
Beispiel #7
0
        public async Task <User> RegisterAsync(User user, string password)
        {
            byte[] passwordHash, passwordSalt;
            CreatePasswordHashSalt(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            await context.AddAsync(user);

            await context.SaveChangesAsync();

            return(user);
        }
Beispiel #8
0
        public static async Task <bool> StoreDisciplinaryPermanentEventAsync(UserDisciplinaryPermanentStorage obj, UserStorage user)
        {
            try
            {
                using (UserContext db = new UserContext())
                {
                    if (!await db.UserStorageTable.AsQueryable().AnyAsync(x => x.UserID == user.UserID))
                    {
                        await db.AddAsync(user);
                    }


                    var existingEvent = await db.UserDisciplinaryPermanentStorageTable.AsQueryable().FirstOrDefaultAsync(x => x.UserID == user.UserID);

                    if (existingEvent != null)
                    {
                        existingEvent = obj;
                        await db.SaveChangesAsync();

                        return(true);
                    }
                    else
                    {
                        await db.AddAsync(obj);

                        await db.SaveChangesAsync();

                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.HandleException(ErrMessages.StorageException, ex);
                throw;
            }
        }
        public async Task AddUser(string userName, string password)
        {
            var user = new Entities.User
            {
                Name = userName,
            };

            var hasher = new PasswordHasher <Entities.User>();

            user.PasswordHash = hasher.HashPassword(user, password);

            await dbContext.AddAsync(user);

            await dbContext.SaveChangesAsync();
        }
Beispiel #10
0
        public async Task <User> CreateAsync(string name, string password)
        {
            try
            {
                var user = await _context.AddAsync(new User { UserName = name, PassWord = password });

                await _context.SaveChangesAsync();

                _logger.LogInformation("User Created with Name {0}", name);
                return(user.Entity);
            }
            catch (Exception e)
            {
                var errorMessage = string.Format("User not created: {0}", e.InnerException.Message);
                _logger.LogError(errorMessage);
                throw (new Exception(errorMessage));
            };
        }
Beispiel #11
0
        public async Task <TEntity> AddAsync(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(AddAsync)} entity is null");
            }

            try
            {
                await _userContext.AddAsync(entity);

                await _userContext.SaveChangesAsync();

                return(entity);
            }
            catch (Exception)
            {
                throw new Exception($"{nameof(entity)} could not be saved");
            }
        }
Beispiel #12
0
        /// <summary>
        /// Sets the channel id the bot is allowed to talk in
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public async Task <int> SetBotChatChannel(ITextChannel channel)
        {
            try
            {
                using (var db = new UserContext()) {
                    var server = await db.Servers.AsAsyncEnumerable().Where(x => x.GuildId == channel.GuildId.ToString()).FirstOrDefaultAsync();

                    if (server == null)
                    {
                        // If the server is not in the database then add it
                        var newServer = new Server();
                        newServer.ChannelId = channel.Id.ToString();
                        newServer.GuildId   = channel.GuildId.ToString();
                        await db.AddAsync(newServer);

                        await db.SaveChangesAsync();

                        return(0);
                    }
                    else
                    {
                        server.GuildId   = channel.GuildId.ToString();
                        server.ChannelId = channel.Id.ToString();
                        await db.SaveChangesAsync();

                        return(0);
                    }
                }
            } catch (Exception e)
            {
#if DEBUG
                Console.WriteLine(e);
#endif
                return(-1);
            }
        }
Beispiel #13
0
        public async Task InsertUserAsync(User user)
        {
            await _dbContext.AddAsync(user);

            await _dbContext.SaveChangesAsync();
        }