Exemplo n.º 1
0
        public async Task <GetUserPayload> FindUserAsync(
            [ID(nameof(User))] int?id,
            [GlobalStateAttribute("currentUserId")] int?currentUserId,
            UserByIdDataLoader dataLoader,
            CancellationToken cancellationToken)
        {
            if (currentUserId == null)
            {
                return(new GetUserPayload(
                           new UserError("Please sign in and try again", "NOT_AUTHORIZED")));
            }

            User signedInUser = await dataLoader.LoadAsync(currentUserId.GetValueOrDefault(), cancellationToken);

            User?foundUser = null;

            if (id != null)
            {
                foundUser = await dataLoader.LoadAsync(id.GetValueOrDefault(), cancellationToken);
            }

            if (foundUser != null && (foundUser.FamilyId != signedInUser.FamilyId))
            {
                return(new GetUserPayload(
                           new UserError("Not allowed to view other families", "NOT_AUTHORIZED")));
            }


            return(new GetUserPayload(foundUser ?? signedInUser));
        }
Exemplo n.º 2
0
        public async Task <AddParticipantPayload> GetAddParticipantAsync(
            AddParticipantInput input,
            [ScopedService] ApplicationDbContext dbContext,
            [Service] IHttpContextAccessor contextAccessor,
            UserByIdDataLoader userById,
            ChatByIdDataLoader chatById,
            CancellationToken ct
            )
        {
            var user = await userById.LoadAsync(input.UserId, ct) ?? throw new Exception("User not found.");

            var chat = await chatById.LoadAsync(input.ChatId, ct) ?? throw new Exception("Chat not found.");

            // Todo: Check if user is allowed to add participants --> add something like a groupadmin

            var userChat = new UserChat
            {
                UserId = user.Id,
                ChatId = chat.Id
            };

            chat.UserChats.Add(userChat);

            await dbContext.SaveChangesAsync();

            return(new AddParticipantPayload(chat));
        }
Exemplo n.º 3
0
        public async Task<User> GetUserAsync([Service]IHttpContextAccessor contextAccessor, UserByIdDataLoader userById, CancellationToken ct)
        {
            var httpContext = contextAccessor.HttpContext ?? throw new ArgumentNullException($"{nameof(contextAccessor.HttpContext)} can't be null.");

            var id = httpContext.User.Claims.Where(uc => uc.Type == "sub").FirstOrDefault();
            return await userById.LoadAsync(id.Value, ct);
        }
Exemplo n.º 4
0
        public Query(IUserService userService, IAvailabilityService availabilityService, UserByIdDataLoader userLoader, IHttpContextAccessor context)
        {
            Field <UserType, User>().Name("user").Argument <NonNullGraphType <StringGraphType> >("id").ResolveAsync(
                x => userLoader.LoadAsync(x.GetArgument <string>("id"))).Authorize();

            FieldAsync <UserType, User>("userByLogin",
                                        arguments: new QueryArguments(new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "login"
            }),
                                        resolve: x => userService.FindByLogin(x.GetArgument <string>("login"))).Authorize();

            FieldAsync <UserType, User>("userByEmail",
                                        arguments: new QueryArguments(new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "email"
            }),
                                        resolve: x => userService.FindByEmail(x.GetArgument <string>("email"))).Authorize();

            FieldAsync <NonNullGraphType <UserType>, User>("me",
                                                           resolve: x => userService.FindById(context.GetUserId())).Authorize();

            FieldAsync <NonNullGraphType <AvailabilityResultType>, AvailabilityResponse>("availabilityByUsername",
                                                                                         arguments: new QueryArguments(new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "username"
            }),
                                                                                         resolve: x => availabilityService.AvailabilityByLogin(x.GetArgument <string>("username")));

            FieldAsync <NonNullGraphType <AvailabilityResultType>, AvailabilityResponse>("availabilityByEmail",
                                                                                         arguments: new QueryArguments(new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "email"
            }),
                                                                                         resolve: x => availabilityService.AvailabilityByEmail(x.GetArgument <string>("email")));
        }
Exemplo n.º 5
0
        public async Task <AddUserPayload> AddUserAsync(
            AddUserInput input,
            [ScopedService] ApplicationDbContext dbContext,
            [Service] IHttpContextAccessor contextAccessor,
            UserByIdDataLoader userById,
            CancellationToken ct
            )
        {
            var httpContext = contextAccessor.HttpContext ?? throw new ArgumentNullException($"{contextAccessor.HttpContext} can't be null.");
            var userId      = httpContext.User.Claims.Where(uc => uc.Type == "sub").FirstOrDefault().Value;

            var dbUser = await userById.LoadAsync(userId, ct);

            if (dbUser != null)
            {
                return(new AddUserPayload(dbUser));
            }

            var user = new User
            {
                Id       = userId,
                Username = input.Username
            };

            dbContext.Users.Add(user);
            await dbContext.SaveChangesAsync();

            return(new AddUserPayload(user));
        }
Exemplo n.º 6
0
            public async Task <IReadOnlyList <UserModel> > GetUsers(RoleModel roleModel, UserByIdDataLoader dataLoader, [ScopedService] LibraryDbContext context, CancellationToken cancellationToken)
            {
                int[] roleIds = await context.Users
                                .Where(x => x.RoleId == roleModel.Id)
                                .Select(x => x.Id)
                                .ToArrayAsync();

                return(await dataLoader.LoadAsync(roleIds, cancellationToken));
            }
Exemplo n.º 7
0
        public UserType(SessionByUserDataLoader sessionLoader, UserByIdDataLoader userLoader)
        {
            Name = "User";
            Key("id");
            Field("id", x => x.Id).Description("user id");
            Field("username", x => x.UserName).Description("username");
            Field("email", x => x.Email).Description("email");
            Field("email_confirmed", x => x.EmailConfirmed).Description("email confirmed");
            Field("lockout_end", x => x.LockoutEnd, true).Description("point at which lockout ends");
            Field <NonNullGraphType <ListGraphType <RefreshTokenType> >, IEnumerable <RefreshToken> >().Name("sessions")
            .ResolveAsync(x => sessionLoader.LoadAsync(x.Source.Id));

            ResolveReferenceAsync(async ctx =>
            {
                var id = ctx.Arguments["id"].ToString();
                return(await userLoader.LoadAsync(id).GetResultAsync());
            });
        }
Exemplo n.º 8
0
 public Task <User> GetUserByIdAsync(
     [ID(nameof(User))] int id,
     UserByIdDataLoader userById,
     CancellationToken cancellationToken) =>
 userById.LoadAsync(id, cancellationToken);
Exemplo n.º 9
0
 public Task <User> GetUser(
     int id,
     UserByIdDataLoader dataLoader,
     CancellationToken cancellationToken) =>
 dataLoader.LoadAsync(id, cancellationToken);
Exemplo n.º 10
0
 public async Task <User> GetUserAsync([ID(nameof(User))] int id, UserByIdDataLoader dataLoader, CancellationToken cancellationToken) =>
 await dataLoader.LoadAsync(id, cancellationToken);