public static async Task <bool> RegisterUser(string username, string password)
        {
            using (Data.Contexts.SMEManagementContextData db = new Contexts.SMEManagementContextData())
            {
                Models.Authentication.User user = await
                                                      (from current in db.Users.Include(".Roles")
                                                      where current.Name == username
                                                      select current).FirstOrDefaultAsync();

                if (user == null)
                {
                    user = new Models.Authentication.User()
                    {
                        Name     = username,
                        Password = Functionalities.Cryptography.HashPassword(password)
                    };

                    await db.Users.AddAsync(user);

                    await db.SaveChangesAsync();

                    return(true);
                }

                return(false);
            }
        }
        public static async Task <bool> ResetSenha(string username, string newPassword)
        {
            using (Data.Contexts.SMEManagementContextData db = new Contexts.SMEManagementContextData())
            {
                Models.Authentication.User user = await
                                                      (from current in db.Users
                                                      where current.Name == username
                                                      select current).FirstOrDefaultAsync();

                if (user != null)
                {
                    user.Password = Functionalities.Cryptography.HashPassword(newPassword);
                    await db.SaveChangesAsync();

                    return(true);
                }
            }

            return(false);
        }
        public static async Task <bool> SetRole(string username, string roleName, string accessLevelValue, Guid perfil)
        {
            using (Contexts.SMEManagementContextData db = new Contexts.SMEManagementContextData())
            {
                var user = await
                               (from current in db.Users
                               where current.Name.Equals(username)
                               select current).FirstOrDefaultAsync();

                if (user == null)
                {
                    return(false);
                }

                var role = await
                               (from current in db.Roles
                               where current.Name.Equals(roleName)
                               select current).FirstOrDefaultAsync();

                if (role == null)
                {
                    return(false);
                }

                var accessLevel = await
                                      (from current in db.AccessLevels
                                      where current.Value.Equals(accessLevelValue)
                                      select current).FirstOrDefaultAsync();

                if (accessLevel == null)
                {
                    return(false);
                }

                var userRole = await
                                   (from current in db.UserRoles
                                   where current.AccessLevelId.Equals(accessLevel.Id) &&
                                   current.RoleId.Equals(role.Id) &&
                                   current.UserId.Equals(user.Id) &&
                                   current.PerfilId.Equals(perfil)
                                   select current).FirstOrDefaultAsync();

                if (userRole != null)
                {
                    return(true);
                }

                userRole = new Models.Authentication.UserRole()
                {
                    User        = user,
                    Role        = role,
                    AccessLevel = accessLevel,
                    PerfilId    = perfil
                };

                await db.UserRoles.AddAsync(userRole);

                await db.SaveChangesAsync();

                return(true);
            }
        }