Update updates fields for the given user.
(ctx context.Context, userID int64, opts UpdateUserOptions)
| 914 | |
| 915 | // Update updates fields for the given user. |
| 916 | func (s *UsersStore) Update(ctx context.Context, userID int64, opts UpdateUserOptions) error { |
| 917 | updates := map[string]any{ |
| 918 | "updated_unix": s.db.NowFunc().Unix(), |
| 919 | } |
| 920 | |
| 921 | if opts.LoginSource != nil { |
| 922 | updates["login_source"] = *opts.LoginSource |
| 923 | } |
| 924 | if opts.LoginName != nil { |
| 925 | updates["login_name"] = *opts.LoginName |
| 926 | } |
| 927 | |
| 928 | if opts.Password != nil { |
| 929 | salt, err := userutil.RandomSalt() |
| 930 | if err != nil { |
| 931 | return errors.Wrap(err, "generate salt") |
| 932 | } |
| 933 | updates["salt"] = salt |
| 934 | updates["passwd"] = userutil.EncodePassword(*opts.Password, salt) |
| 935 | opts.GenerateNewRands = true |
| 936 | } |
| 937 | if opts.GenerateNewRands { |
| 938 | rands, err := userutil.RandomSalt() |
| 939 | if err != nil { |
| 940 | return errors.Wrap(err, "generate rands") |
| 941 | } |
| 942 | updates["rands"] = rands |
| 943 | } |
| 944 | |
| 945 | if opts.FullName != nil { |
| 946 | updates["full_name"] = strutil.Truncate(*opts.FullName, 255) |
| 947 | } |
| 948 | if opts.Email != nil { |
| 949 | _, err := s.GetByEmail(ctx, *opts.Email) |
| 950 | if err == nil { |
| 951 | return ErrEmailAlreadyUsed{args: errutil.Args{"email": *opts.Email}} |
| 952 | } else if !IsErrUserNotExist(err) { |
| 953 | return errors.Wrap(err, "check email") |
| 954 | } |
| 955 | updates["email"] = *opts.Email |
| 956 | } |
| 957 | if opts.Website != nil { |
| 958 | updates["website"] = strutil.Truncate(*opts.Website, 255) |
| 959 | } |
| 960 | if opts.Location != nil { |
| 961 | updates["location"] = strutil.Truncate(*opts.Location, 255) |
| 962 | } |
| 963 | if opts.Description != nil { |
| 964 | updates["description"] = strutil.Truncate(*opts.Description, 255) |
| 965 | } |
| 966 | |
| 967 | if opts.MaxRepoCreation != nil { |
| 968 | if *opts.MaxRepoCreation < -1 { |
| 969 | *opts.MaxRepoCreation = -1 |
| 970 | } |
| 971 | updates["max_repo_creation"] = *opts.MaxRepoCreation |
| 972 | } |
| 973 | if opts.LastRepoVisibility != nil { |
nothing calls this directly
no test coverage detected