This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathuser.go
More file actions
941 lines (782 loc) · 27.4 KB
/
user.go
File metadata and controls
941 lines (782 loc) · 27.4 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
package graphqlbackend
import (
"context"
"net/url"
"sync"
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/cody"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/ssc"
"github.com/sourcegraph/sourcegraph/internal/dotcom"
"github.com/keegancsmith/sqlf"
"github.com/sourcegraph/log"
"github.com/sourcegraph/sourcegraph/cmd/frontend/backend"
"github.com/sourcegraph/sourcegraph/cmd/frontend/graphqlbackend/graphqlutil"
"github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/auth"
"github.com/sourcegraph/sourcegraph/internal/auth/providers"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/errcode"
"github.com/sourcegraph/sourcegraph/internal/featureflag"
"github.com/sourcegraph/sourcegraph/internal/gqlutil"
"github.com/sourcegraph/sourcegraph/internal/suspiciousnames"
"github.com/sourcegraph/sourcegraph/internal/types"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
func (r *schemaResolver) User(
ctx context.Context,
args struct {
Username *string
Email *string
DatabaseID *int32
},
) (*UserResolver, error) {
var err error
var user *types.User
switch {
case args.Username != nil:
user, err = r.db.Users().GetByUsername(ctx, *args.Username)
case args.DatabaseID != nil:
user, err = r.db.Users().GetByID(ctx, *args.DatabaseID)
case args.Email != nil:
// 🚨 SECURITY: Only site admins are allowed to look up by email address on
// Sourcegraph.com, for user privacy reasons.
if dotcom.SourcegraphDotComMode() {
if err := auth.CheckCurrentUserIsSiteAdmin(ctx, r.db); err != nil {
return nil, err
}
}
user, err = r.db.Users().GetByVerifiedEmail(ctx, *args.Email)
default:
return nil, errors.New("must specify either username or email to look up a user")
}
if err != nil {
if errcode.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return NewUserResolver(ctx, r.db, user), nil
}
// UserResolver implements the GraphQL User type.
type UserResolver struct {
logger log.Logger
db database.DB
user *types.User
// actor containing current user (derived from request context), which lets us
// skip fetching actor from context in every resolver function and save DB calls,
// because user is fetched in actor only once.
actor *actor.Actor
codySubscription *cody.UserSubscription
fetchCodySubscriptionError error
fetchCodySubscriptionOnce sync.Once
}
// NewUserResolver returns a new UserResolver with given user object.
func NewUserResolver(ctx context.Context, db database.DB, user *types.User) *UserResolver {
return &UserResolver{
db: db,
user: user,
logger: log.Scoped("userResolver").With(log.String("user", user.Username)),
actor: actor.FromContext(ctx),
}
}
// newUserResolverFromActor returns a new UserResolver with given user object.
func newUserResolverFromActor(a *actor.Actor, db database.DB, user *types.User) *UserResolver {
return &UserResolver{
db: db,
user: user,
logger: log.Scoped("userResolver").With(log.String("user", user.Username)),
actor: a,
}
}
// UserByID looks up and returns the user with the given GraphQL ID. If no such user exists, it returns a
// non-nil error.
func UserByID(ctx context.Context, db database.DB, id graphql.ID) (*UserResolver, error) {
userID, err := UnmarshalUserID(id)
if err != nil {
return nil, err
}
return UserByIDInt32(ctx, db, userID)
}
// UserByIDInt32 looks up and returns the user with the given database ID. If no such user exists,
// it returns a non-nil error.
func UserByIDInt32(ctx context.Context, db database.DB, id int32) (*UserResolver, error) {
user, err := db.Users().GetByID(ctx, id)
if err != nil {
return nil, err
}
return NewUserResolver(ctx, db, user), nil
}
func (r *UserResolver) ID() graphql.ID { return MarshalUserID(r.user.ID) }
func MarshalUserID(id int32) graphql.ID { return relay.MarshalID("User", id) }
func UnmarshalUserID(id graphql.ID) (userID int32, err error) {
err = relay.UnmarshalSpec(id, &userID)
return
}
// DatabaseID returns the numeric ID for the user in the database.
func (r *UserResolver) DatabaseID() int32 { return r.user.ID }
// Email returns the user's oldest email, if one exists.
// Deprecated: use Emails instead.
func (r *UserResolver) Email(ctx context.Context) (string, error) {
// 🚨 SECURITY: Only the user and admins are allowed to access the email address.
if err := auth.CheckSiteAdminOrSameUserFromActor(r.actor, r.db, r.user.ID); err != nil {
return "", err
}
email, _, err := r.db.UserEmails().GetPrimaryEmail(ctx, r.user.ID)
if err != nil && !errcode.IsNotFound(err) {
return "", err
}
return email, nil
}
func (r *UserResolver) Username() string { return r.user.Username }
func (r *UserResolver) DisplayName() *string {
if r.user.DisplayName == "" {
return nil
}
return &r.user.DisplayName
}
func (r *UserResolver) BuiltinAuth() bool {
return r.user.BuiltinAuth && providers.BuiltinAuthEnabled()
}
func (r *UserResolver) AvatarURL() *string {
if r.user.AvatarURL == "" {
return nil
}
return &r.user.AvatarURL
}
func (r *UserResolver) URL() string {
return "/users/" + r.user.Username
}
func (r *UserResolver) SettingsURL() *string { return strptr(r.URL() + "/settings") }
type CodySubscriptionResolver struct {
subscription *cody.UserSubscription
}
func (r *CodySubscriptionResolver) Status() ssc.SubscriptionStatus {
return r.subscription.Status
}
func (r *CodySubscriptionResolver) Plan() cody.UserSubscriptionPlan {
return r.subscription.Plan
}
func (r *CodySubscriptionResolver) ApplyProRateLimits() bool {
return r.subscription.ApplyProRateLimits
}
func (r *CodySubscriptionResolver) CurrentPeriodStartAt() gqlutil.DateTime {
return gqlutil.DateTime{Time: r.subscription.CurrentPeriodStartAt}
}
func (r *CodySubscriptionResolver) CurrentPeriodEndAt() gqlutil.DateTime {
return gqlutil.DateTime{Time: r.subscription.CurrentPeriodEndAt}
}
func (r *CodySubscriptionResolver) CancelAtPeriodEnd() bool {
return r.subscription.CancelAtPeriodEnd
}
func (r *UserResolver) fetchCodySubscription(ctx context.Context) (*cody.UserSubscription, error) {
r.fetchCodySubscriptionOnce.Do(func() {
subscription, err := cody.SubscriptionForUser(ctx, r.db, *r.user)
if err != nil {
r.fetchCodySubscriptionError = err
return
}
r.codySubscription = subscription
})
if r.fetchCodySubscriptionError != nil {
return nil, r.fetchCodySubscriptionError
}
return r.codySubscription, r.fetchCodySubscriptionError
}
func (r *UserResolver) CodySubscription(ctx context.Context) (*CodySubscriptionResolver, error) {
if !dotcom.SourcegraphDotComMode() {
return nil, errors.New("this feature is only available on sourcegraph.com")
}
subscription, err := r.fetchCodySubscription(ctx)
if err != nil {
return nil, err
}
return &CodySubscriptionResolver{subscription: subscription}, nil
}
func (r *UserResolver) CreatedAt() gqlutil.DateTime {
return gqlutil.DateTime{Time: r.user.CreatedAt}
}
func (r *UserResolver) CodyProEnabled(ctx context.Context) (bool, error) {
if !dotcom.SourcegraphDotComMode() {
return false, errors.New("this feature is only available on sourcegraph.com")
}
subscription, err := r.fetchCodySubscription(ctx)
if err != nil {
return false, err
}
return subscription.Plan == cody.UserSubscriptionPlanPro, nil
}
func (r *UserResolver) CodyCurrentPeriodChatLimit(ctx context.Context) (int32, error) {
if !dotcom.SourcegraphDotComMode() {
return 0, errors.New("this feature is only available on sourcegraph.com")
}
subscription, err := r.fetchCodySubscription(ctx)
if err != nil {
return 0, err
}
if subscription.ApplyProRateLimits {
return 0, nil
}
cfg := conf.GetCompletionsConfig(conf.Get().SiteConfig())
limit := int32(cfg.PerCommunityUserChatMonthlyInteractionLimit)
return limit, nil
}
func (r *UserResolver) CodyCurrentPeriodCodeLimit(ctx context.Context) (int32, error) {
if !dotcom.SourcegraphDotComMode() {
return 0, errors.New("this feature is only available on sourcegraph.com")
}
// TODO(sqs): This is not enforced anymore as the intent is to give free unlimited autocomplete.
subscription, err := r.fetchCodySubscription(ctx)
if err != nil {
return 0, err
}
if subscription.ApplyProRateLimits {
return 0, nil
}
cfg := conf.GetCompletionsConfig(conf.Get().SiteConfig())
limit := int32(cfg.PerCommunityUserCodeCompletionsMonthlyInteractionLimit)
return limit, nil
}
func (r *UserResolver) CodyCurrentPeriodChatUsage(ctx context.Context) (int32, error) {
if !dotcom.SourcegraphDotComMode() {
return 0, errors.New("this feature is only available on sourcegraph.com")
}
subscription, err := r.fetchCodySubscription(ctx)
if err != nil {
return 0, err
}
query := sqlf.Sprintf(`
WHERE
user_id = %s
AND timestamp >= %s
AND timestamp <= NOW()
AND name NOT LIKE '%%clicked%%'
AND name NOT LIKE '%%resetChat%%'
AND name NOT LIKE '%%menu:opened%%'
AND name NOT LIKE 'cody.%%'
AND (
(
SOURCE = 'IDEEXTENSION'
AND (
name LIKE '%%recipe%%'
OR name LIKE '%%command%%'
OR name LIKE '%%chat:executed%%'
OR name LIKE '%%chat-question:executed%%'
OR name LIKE '%%inline-chat%%'
OR name LIKE '%%inline-assist%%'
OR name LIKE '%%chat:submitted%%'
)
)
OR
(
SOURCE = 'WEB'
AND (
name = 'web:codyChat:submit'
OR name LIKE 'web:codyChat:recipe:%%'
)
)
)
`, r.user.ID, subscription.CurrentPeriodStartAt)
count, err := r.db.EventLogs().CountBySQL(ctx, query)
return int32(count), err
}
func (r *UserResolver) CodyCurrentPeriodCodeUsage(ctx context.Context) (int32, error) {
if !dotcom.SourcegraphDotComMode() {
return 0, errors.New("this feature is only available on sourcegraph.com")
}
subscription, err := r.fetchCodySubscription(ctx)
if err != nil {
return 0, err
}
query := sqlf.Sprintf(`
WHERE
user_id = %s
AND timestamp >= %s
AND timestamp <= NOW()
AND source = 'IDEEXTENSION'
AND name LIKE '%%completion:suggested%%'
`, r.user.ID, subscription.CurrentPeriodStartAt)
count, err := r.db.EventLogs().CountBySQL(ctx, query)
return int32(count), err
}
func (r *UserResolver) UpdatedAt() *gqlutil.DateTime {
return &gqlutil.DateTime{Time: r.user.UpdatedAt}
}
func (r *UserResolver) settingsSubject() api.SettingsSubject {
return api.SettingsSubject{User: &r.user.ID}
}
func (r *UserResolver) LatestSettings(ctx context.Context) (*settingsResolver, error) {
// 🚨 SECURITY: Only the user and admins are allowed to access the user's
// settings, because they may contain secrets or other sensitive data.
if err := auth.CheckSiteAdminOrSameUserFromActor(r.actor, r.db, r.user.ID); err != nil {
return nil, err
}
settings, err := r.db.Settings().GetLatest(ctx, r.settingsSubject())
if err != nil {
return nil, err
}
if settings == nil {
return nil, nil
}
return &settingsResolver{r.db, &settingsSubjectResolver{user: r}, settings, nil}, nil
}
func (r *UserResolver) SettingsCascade() *settingsCascade {
return &settingsCascade{db: r.db, subject: &settingsSubjectResolver{user: r}}
}
func (r *UserResolver) SiteAdmin() (bool, error) {
// 🚨 SECURITY: Only the user and admins are allowed to determine if the user is a site admin.
if err := auth.CheckSiteAdminOrSameUserFromActor(r.actor, r.db, r.user.ID); err != nil {
return false, err
}
return r.user.SiteAdmin, nil
}
func (r *UserResolver) TosAccepted(_ context.Context) bool {
return r.user.TosAccepted
}
type updateUserArgs struct {
User graphql.ID
Username *string
DisplayName *string
AvatarURL *string
}
func (r *schemaResolver) UpdateUser(ctx context.Context, args *updateUserArgs) (*UserResolver, error) {
userID, err := UnmarshalUserID(args.User)
if err != nil {
return nil, err
}
// 🚨 SECURITY: Only the user and site admins are allowed to update the user.
if err := auth.CheckSiteAdminOrSameUser(ctx, r.db, userID); err != nil {
return nil, err
}
if args.Username != nil {
if err := suspiciousnames.CheckNameAllowedForUserOrOrganization(*args.Username); err != nil {
return nil, err
}
}
if args.AvatarURL != nil && len(*args.AvatarURL) > 0 {
if len(*args.AvatarURL) > 3000 {
return nil, errors.New("avatar URL exceeded 3000 characters")
}
u, err := url.Parse(*args.AvatarURL)
if err != nil {
return nil, errors.Wrap(err, "unable to parse avatar URL")
} else if u.Scheme != "http" && u.Scheme != "https" {
return nil, errors.New("avatar URL must be an HTTP or HTTPS URL")
}
}
update := database.UserUpdate{
AvatarURL: args.AvatarURL,
}
user, err := r.db.Users().GetByID(ctx, userID)
if err != nil {
return nil, errors.Wrap(err, "getting user from the database")
}
usernameChanged := args.Username != nil && user.Username != *args.Username
// Check if the user account is SCIM controlled
if user.SCIMControlled {
errUserScimManaged := errors.Errorf("cannot update externally managed user")
if usernameChanged {
return nil, errUserScimManaged
}
if args.DisplayName != nil && user.DisplayName != *args.DisplayName {
return nil, errUserScimManaged
}
}
update.DisplayName = args.DisplayName
// If user is changing their username, we need to verify if this action can be
// done.
if usernameChanged {
if !viewerCanChangeUsername(actor.FromContext(ctx), r.db, userID) {
return nil, errors.Errorf("unable to change username because auth.enableUsernameChanges is false in site configuration")
}
update.Username = *args.Username
}
if err := r.db.Users().Update(ctx, userID, update); err != nil {
return nil, err
}
if featureflag.FromContext(ctx).GetBoolOr("auditlog-expansion", false) {
// Log an event when a user account is modified/updated
if err := r.db.SecurityEventLogs().LogSecurityEvent(ctx, database.SecurityEventNameAccountModified, "", uint32(userID), "", "BACKEND", args); err != nil {
r.logger.Error("Error logging security event", log.Error(err))
}
}
return UserByIDInt32(ctx, r.db, userID)
}
type changeCodyPlanArgs struct {
User graphql.ID
Pro bool
}
func (r *schemaResolver) ChangeCodyPlan(ctx context.Context, args *changeCodyPlanArgs) (*UserResolver, error) {
if !dotcom.SourcegraphDotComMode() {
return nil, errors.New("this feature is only available on sourcegraph.com")
}
userID, err := UnmarshalUserID(args.User)
if err != nil {
return nil, err
}
// 🚨 SECURITY: Only the authenticated user can update their properties.
if err := auth.CheckSameUser(ctx, userID); err != nil {
return nil, err
}
if err := r.db.Users().ChangeCodyPlan(ctx, userID, args.Pro); err != nil {
return nil, err
}
if err, _ := cody.RefreshGatewayRateLimits(ctx, userID, r.db); err != nil {
// We intentionally don't fail the upgrade flow here, Gateway will pickup
// the new limits automatically. (Just later than we'd like.)
r.logger.Warn("refresh gateway limits", log.Error(err))
}
return UserByIDInt32(ctx, r.db, userID)
}
// CurrentUser returns the authenticated user if any. If there is no authenticated user, it returns
// (nil, nil). If some other error occurs, then the error is returned.
func CurrentUser(ctx context.Context, db database.DB) (*UserResolver, error) {
user, err := db.Users().GetByCurrentAuthUser(ctx)
if err != nil {
if errcode.IsNotFound(err) || err == database.ErrNoCurrentUser {
return nil, nil
}
return nil, err
}
return newUserResolverFromActor(actor.FromActualUser(user), db, user), nil
}
func (r *UserResolver) Organizations(ctx context.Context) (*orgConnectionStaticResolver, error) {
// 🚨 SECURITY: Only the user and admins are allowed to access the user's
// organisations.
if err := auth.CheckSiteAdminOrSameUserFromActor(r.actor, r.db, r.user.ID); err != nil {
return nil, err
}
orgs, err := r.db.Orgs().GetByUserID(ctx, r.user.ID)
if err != nil {
return nil, err
}
c := orgConnectionStaticResolver{nodes: make([]*OrgResolver, len(orgs))}
for i, org := range orgs {
c.nodes[i] = &OrgResolver{r.db, org}
}
return &c, nil
}
func (r *UserResolver) SurveyResponses(ctx context.Context) ([]*surveyResponseResolver, error) {
// 🚨 SECURITY: Only the user and admins are allowed to access the user's survey responses.
if err := auth.CheckSiteAdminOrSameUserFromActor(r.actor, r.db, r.user.ID); err != nil {
return nil, err
}
responses, err := database.SurveyResponses(r.db).GetByUserID(ctx, r.user.ID)
if err != nil {
return nil, err
}
surveyResponseResolvers := make([]*surveyResponseResolver, 0, len(responses))
for _, response := range responses {
surveyResponseResolvers = append(surveyResponseResolvers, &surveyResponseResolver{r.db, response})
}
return surveyResponseResolvers, nil
}
func (r *UserResolver) ViewerCanAdminister() (bool, error) {
err := auth.CheckSiteAdminOrSameUserFromActor(r.actor, r.db, r.user.ID)
if errcode.IsUnauthorized(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}
func (r *UserResolver) viewerCanAdministerSettings() (bool, error) {
err := auth.CheckSiteAdminOrSameUserFromActor(r.actor, r.db, r.user.ID)
if errcode.IsUnauthorized(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}
func (r *UserResolver) NamespaceName() string { return r.user.Username }
func (r *UserResolver) SCIMControlled() bool { return r.user.SCIMControlled }
func (r *UserResolver) PermissionsInfo(ctx context.Context) (PermissionsInfoResolver, error) {
return EnterpriseResolvers.authzResolver.UserPermissionsInfo(ctx, r.ID())
}
func (r *schemaResolver) UpdatePassword(ctx context.Context, args *struct {
OldPassword string
NewPassword string
},
) (*EmptyResponse, error) {
// 🚨 SECURITY: Only the authenticated user can change their password.
user, err := r.db.Users().GetByCurrentAuthUser(ctx)
if err != nil {
return nil, err
}
if user == nil {
return nil, errors.New("no authenticated user")
}
if err := r.db.Users().UpdatePassword(ctx, user.ID, args.OldPassword, args.NewPassword); err != nil {
return nil, err
}
logger := r.logger.Scoped("UpdatePassword").
With(log.Int32("userID", user.ID))
if conf.CanSendEmail() {
if err := backend.NewUserEmailsService(r.db, logger).SendUserEmailOnFieldUpdate(ctx, user.ID, "updated the password"); err != nil {
logger.Warn("Failed to send email to inform user of password update", log.Error(err))
}
}
return &EmptyResponse{}, nil
}
func (r *schemaResolver) CreatePassword(ctx context.Context, args *struct {
NewPassword string
},
) (*EmptyResponse, error) {
// 🚨 SECURITY: Only the authenticated user can create their password.
if !actor.FromContext(ctx).FromSessionCookie {
return nil, errors.New("only allowed from user session")
}
user, err := r.db.Users().GetByCurrentAuthUser(ctx)
if err != nil {
return nil, err
}
if user == nil {
return nil, errors.New("no authenticated user")
}
if err := r.db.Users().CreatePassword(ctx, user.ID, args.NewPassword); err != nil {
return nil, err
}
logger := r.logger.Scoped("CreatePassword").
With(log.Int32("userID", user.ID))
if conf.CanSendEmail() {
if err := backend.NewUserEmailsService(r.db, logger).SendUserEmailOnFieldUpdate(ctx, user.ID, "created a password"); err != nil {
logger.Warn("Failed to send email to inform user of password creation", log.Error(err))
}
}
return &EmptyResponse{}, nil
}
// userMutationArgs hold an optional user ID for mutations that take in a user ID
// or assume the current user when no ID is given.
type userMutationArgs struct {
UserID *graphql.ID
}
func (r *schemaResolver) affectedUserID(ctx context.Context, args *userMutationArgs) (affectedUserID int32, err error) {
if args.UserID != nil {
return UnmarshalUserID(*args.UserID)
}
user, err := r.db.Users().GetByCurrentAuthUser(ctx)
if err != nil {
return 0, err
}
return user.ID, nil
}
func (r *schemaResolver) SetTosAccepted(ctx context.Context, args *userMutationArgs) (*EmptyResponse, error) {
affectedUserID, err := r.affectedUserID(ctx, args)
if err != nil {
return nil, err
}
tosAccepted := true
update := database.UserUpdate{TosAccepted: &tosAccepted}
return r.updateAffectedUser(ctx, affectedUserID, update)
}
func (r *schemaResolver) updateAffectedUser(ctx context.Context, affectedUserID int32, update database.UserUpdate) (*EmptyResponse, error) {
// 🚨 SECURITY: Only the user and admins are allowed to set the Terms of Service accepted flag.
if err := auth.CheckSiteAdminOrSameUser(ctx, r.db, affectedUserID); err != nil {
return nil, err
}
if err := r.db.Users().Update(ctx, affectedUserID, update); err != nil {
return nil, err
}
return &EmptyResponse{}, nil
}
// ViewerCanChangeUsername returns if the current user can change the username of the user.
func (r *UserResolver) ViewerCanChangeUsername() bool {
return viewerCanChangeUsername(r.actor, r.db, r.user.ID)
}
func (r *UserResolver) CompletionsQuotaOverride(ctx context.Context) (*int32, error) {
// 🚨 SECURITY: Only the user and admins are allowed to see quotas.
if err := auth.CheckSiteAdminOrSameUser(ctx, r.db, r.user.ID); err != nil {
return nil, err
}
v, err := r.db.Users().GetChatCompletionsQuota(ctx, r.user.ID)
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
iv := int32(*v)
return &iv, nil
}
func (r *UserResolver) CodeCompletionsQuotaOverride(ctx context.Context) (*int32, error) {
// 🚨 SECURITY: Only the user and admins are allowed to see quotas.
if err := auth.CheckSiteAdminOrSameUser(ctx, r.db, r.user.ID); err != nil {
return nil, err
}
v, err := r.db.Users().GetCodeCompletionsQuota(ctx, r.user.ID)
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
iv := int32(*v)
return &iv, nil
}
func (r *UserResolver) BatchChanges(ctx context.Context, args *ListBatchChangesArgs) (BatchChangesConnectionResolver, error) {
id := r.ID()
args.Namespace = &id
return EnterpriseResolvers.batchChangesResolver.BatchChanges(ctx, args)
}
func (r *UserResolver) BatchChangesCodeHosts(ctx context.Context, args *ListBatchChangesCodeHostsArgs) (BatchChangesCodeHostConnectionResolver, error) {
args.UserID = &r.user.ID
return EnterpriseResolvers.batchChangesResolver.BatchChangesCodeHosts(ctx, args)
}
func (r *UserResolver) Roles(ctx context.Context, args *ListRoleArgs) (*graphqlutil.ConnectionResolver[RoleResolver], error) {
// 🚨 SECURITY: In dotcom mode, only allow site admins to check roles.
if dotcom.SourcegraphDotComMode() && auth.CheckCurrentUserIsSiteAdmin(ctx, r.db) != nil {
return nil, errors.New("unauthorized")
}
userID := r.user.ID
connectionStore := &roleConnectionStore{
db: r.db,
userID: userID,
}
return graphqlutil.NewConnectionResolver[RoleResolver](
connectionStore,
&args.ConnectionResolverArgs,
&graphqlutil.ConnectionResolverOptions{
AllowNoLimit: true,
},
)
}
func (r *UserResolver) Permissions() (*graphqlutil.ConnectionResolver[PermissionResolver], error) {
userID := r.user.ID
if err := auth.CheckSiteAdminOrSameUserFromActor(r.actor, r.db, userID); err != nil {
return nil, err
}
connectionStore := &permissionConnectionStore{
db: r.db,
userID: userID,
}
return graphqlutil.NewConnectionResolver[PermissionResolver](
connectionStore,
&graphqlutil.ConnectionResolverArgs{},
&graphqlutil.ConnectionResolverOptions{
AllowNoLimit: true,
},
)
}
func viewerCanChangeUsername(a *actor.Actor, db database.DB, userID int32) bool {
if err := auth.CheckSiteAdminOrSameUserFromActor(a, db, userID); err != nil {
return false
}
if conf.Get().AuthEnableUsernameChanges {
return true
}
// 🚨 SECURITY: Only site admins are allowed to change a user's username when auth.enableUsernameChanges == false.
return auth.CheckCurrentActorIsSiteAdmin(a, db) == nil
}
func (r *UserResolver) Monitors(ctx context.Context, args *ListMonitorsArgs) (MonitorConnectionResolver, error) {
if err := auth.CheckSameUserFromActor(r.actor, r.user.ID); err != nil {
return nil, err
}
return EnterpriseResolvers.codeMonitorsResolver.Monitors(ctx, &r.user.ID, args)
}
func (r *UserResolver) ToUser() (*UserResolver, bool) {
return r, true
}
func (r *UserResolver) OwnerField() string {
return EnterpriseResolvers.ownResolver.UserOwnerField(r)
}
type SetUserCompletionsQuotaArgs struct {
User graphql.ID
Quota *int32
}
func (r *schemaResolver) SetUserCompletionsQuota(ctx context.Context, args SetUserCompletionsQuotaArgs) (*UserResolver, error) {
// 🚨 SECURITY: Only site admins are allowed to change a users quota.
if err := auth.CheckCurrentUserIsSiteAdmin(ctx, r.db); err != nil {
return nil, err
}
requestingActor := actor.FromContext(ctx)
if args.Quota != nil && *args.Quota <= 0 {
return nil, errors.New("quota must be 1 or greater")
}
id, err := UnmarshalUserID(args.User)
if err != nil {
return nil, err
}
// Verify the ID is valid.
user, err := r.db.Users().GetByID(ctx, id)
if err != nil {
return nil, err
}
// Lookup the current quota, so we can log the delta.
oldQuota, err := r.db.Users().GetChatCompletionsQuota(ctx, user.ID)
if err != nil {
return nil, err
}
var newQuota *int
if args.Quota != nil {
i := int(*args.Quota)
newQuota = &i
}
if err := r.db.Users().SetChatCompletionsQuota(ctx, user.ID, newQuota); err != nil {
return nil, err
}
// Log that the user's completions quota was updated.
r.logger.Info("setting user completions quota",
log.Int("requestingUserID", int(requestingActor.UID)),
log.Int("targetUserID", int(user.ID)),
log.Intp("oldQuota", oldQuota),
log.Intp("newQuota", newQuota))
if featureflag.FromContext(ctx).GetBoolOr("auditlog-expansion", false) {
if err := r.db.SecurityEventLogs().LogSecurityEvent(ctx, database.SecurityEventNameUserCompletionQuotaUpdated, "", uint32(id), "", "BACKEND", args); err != nil {
r.logger.Error("Error logging security event", log.Error(err))
}
}
return UserByIDInt32(ctx, r.db, user.ID)
}
type SetUserCodeCompletionsQuotaArgs struct {
User graphql.ID
Quota *int32
}
func (r *schemaResolver) SetUserCodeCompletionsQuota(ctx context.Context, args SetUserCodeCompletionsQuotaArgs) (*UserResolver, error) {
// 🚨 SECURITY: Only site admins are allowed to change a users quota.
if err := auth.CheckCurrentUserIsSiteAdmin(ctx, r.db); err != nil {
return nil, err
}
if args.Quota != nil && *args.Quota <= 0 {
return nil, errors.New("quota must be 1 or greater")
}
id, err := UnmarshalUserID(args.User)
if err != nil {
return nil, err
}
// Verify the ID is valid.
user, err := r.db.Users().GetByID(ctx, id)
if err != nil {
return nil, err
}
var quota *int
if args.Quota != nil {
i := int(*args.Quota)
quota = &i
}
if err := r.db.Users().SetCodeCompletionsQuota(ctx, user.ID, quota); err != nil {
return nil, err
}
if featureflag.FromContext(ctx).GetBoolOr("auditlog-expansion", false) {
// Log an event when user's code completions quota is updated
if err := r.db.SecurityEventLogs().LogSecurityEvent(ctx, database.SecurityEventNameUserCodeCompletionQuotaUpdated, "", uint32(id), "", "BACKEND", args); err != nil {
r.logger.Error("Error logging security event", log.Error(err))
}
}
return UserByIDInt32(ctx, r.db, user.ID)
}
func (r *UserResolver) EvaluateFeatureFlag(ctx context.Context, args *struct {
FlagName string
}) (*bool, error) {
ffs, err := r.db.FeatureFlags().GetUserFlags(ctx, r.user.ID)
if err != nil {
return nil, err
}
if v, ok := ffs[args.FlagName]; ok {
return &v, nil
}
// If there is no value for this feature flag, then we return nil. This follows the existing behaviour from the root level evaluateFeatureFlag function.
return nil, nil
}