MCPcopy
hub / github.com/gogs/gogs / Create

Method Create

internal/database/users.go:292–346  ·  view source on GitHub ↗

Create creates a new user and persists to database. It returns ErrNameNotAllowed if the given name or pattern of the name is not allowed as a username, or ErrUserAlreadyExist when a user with same name already exists, or ErrEmailAlreadyUsed if the email has been verified by another user.

(ctx context.Context, username, email string, opts CreateUserOptions)

Source from the content-addressed store, hash-verified

290// a username, or ErrUserAlreadyExist when a user with same name already exists,
291// or ErrEmailAlreadyUsed if the email has been verified by another user.
292func (s *UsersStore) Create(ctx context.Context, username, email string, opts CreateUserOptions) (*User, error) {
293 err := isUsernameAllowed(username)
294 if err != nil {
295 return nil, err
296 }
297
298 if s.IsUsernameUsed(ctx, username, 0) {
299 return nil, ErrUserAlreadyExist{
300 args: errutil.Args{
301 "name": username,
302 },
303 }
304 }
305
306 email = strings.ToLower(strings.TrimSpace(email))
307 _, err = s.GetByEmail(ctx, email)
308 if err == nil {
309 return nil, ErrEmailAlreadyUsed{
310 args: errutil.Args{
311 "email": email,
312 },
313 }
314 } else if !IsErrUserNotExist(err) {
315 return nil, err
316 }
317
318 user := &User{
319 LowerName: strings.ToLower(username),
320 Name: username,
321 FullName: opts.FullName,
322 Email: email,
323 Password: opts.Password,
324 LoginSource: opts.LoginSource,
325 LoginName: opts.LoginName,
326 Location: opts.Location,
327 Website: opts.Website,
328 MaxRepoCreation: -1,
329 IsActive: opts.Activated,
330 IsAdmin: opts.Admin,
331 Avatar: cryptoutil.MD5(email), // Gravatar URL uses the MD5 hash of the email, see https://en.gravatar.com/site/implement/hash/
332 AvatarEmail: email,
333 }
334
335 user.Rands, err = userutil.RandomSalt()
336 if err != nil {
337 return nil, err
338 }
339 user.Salt, err = userutil.RandomSalt()
340 if err != nil {
341 return nil, err
342 }
343 user.Password = userutil.EncodePassword(user.Password, user.Salt)
344
345 return user, s.db.WithContext(ctx).Create(user).Error
346}
347
348// DeleteCustomAvatar deletes the current user custom avatar and falls back to
349// use look up avatar by email.

Callers 15

AuthenticateMethod · 0.95
ToFileMethod · 0.45
actionsCommitRepoFunction · 0.45
actionsMergePullRequestFunction · 0.45
actionsMirrorSyncCreateFunction · 0.45
actionsMirrorSyncDeleteFunction · 0.45
actionsMirrorSyncPushFunction · 0.45
actionsNewRepoFunction · 0.45
actionsPushTagFunction · 0.45
actionsRenameRepoFunction · 0.45
actionsTransferRepoFunction · 0.45
CreateObjectMethod · 0.45

Calls 7

IsUsernameUsedMethod · 0.95
GetByEmailMethod · 0.95
MD5Function · 0.92
RandomSaltFunction · 0.92
EncodePasswordFunction · 0.92
isUsernameAllowedFunction · 0.85
IsErrUserNotExistFunction · 0.85

Tested by 15

actionsCommitRepoFunction · 0.36
actionsMergePullRequestFunction · 0.36
actionsMirrorSyncCreateFunction · 0.36
actionsMirrorSyncDeleteFunction · 0.36
actionsMirrorSyncPushFunction · 0.36
actionsNewRepoFunction · 0.36
actionsPushTagFunction · 0.36
actionsRenameRepoFunction · 0.36
actionsTransferRepoFunction · 0.36
loginSourcesCreateFunction · 0.36
loginSourcesCountFunction · 0.36
loginSourcesDeleteByIDFunction · 0.36