code.gitea.io/gitea@v1.21.7/services/convert/user.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package convert
     5  
     6  import (
     7  	"context"
     8  
     9  	"code.gitea.io/gitea/models/perm"
    10  	user_model "code.gitea.io/gitea/models/user"
    11  	api "code.gitea.io/gitea/modules/structs"
    12  )
    13  
    14  // ToUser convert user_model.User to api.User
    15  // if doer is set, private information is added if the doer has the permission to see it
    16  func ToUser(ctx context.Context, user, doer *user_model.User) *api.User {
    17  	if user == nil {
    18  		return nil
    19  	}
    20  	authed := false
    21  	signed := false
    22  	if doer != nil {
    23  		signed = true
    24  		authed = doer.ID == user.ID || doer.IsAdmin
    25  	}
    26  	return toUser(ctx, user, signed, authed)
    27  }
    28  
    29  // ToUsers convert list of user_model.User to list of api.User
    30  func ToUsers(ctx context.Context, doer *user_model.User, users []*user_model.User) []*api.User {
    31  	result := make([]*api.User, len(users))
    32  	for i := range users {
    33  		result[i] = ToUser(ctx, users[i], doer)
    34  	}
    35  	return result
    36  }
    37  
    38  // ToUserWithAccessMode convert user_model.User to api.User
    39  // AccessMode is not none show add some more information
    40  func ToUserWithAccessMode(ctx context.Context, user *user_model.User, accessMode perm.AccessMode) *api.User {
    41  	if user == nil {
    42  		return nil
    43  	}
    44  	return toUser(ctx, user, accessMode != perm.AccessModeNone, false)
    45  }
    46  
    47  // toUser convert user_model.User to api.User
    48  // signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself
    49  func toUser(ctx context.Context, user *user_model.User, signed, authed bool) *api.User {
    50  	result := &api.User{
    51  		ID:          user.ID,
    52  		UserName:    user.Name,
    53  		FullName:    user.FullName,
    54  		Email:       user.GetPlaceholderEmail(),
    55  		AvatarURL:   user.AvatarLink(ctx),
    56  		Created:     user.CreatedUnix.AsTime(),
    57  		Restricted:  user.IsRestricted,
    58  		Location:    user.Location,
    59  		Website:     user.Website,
    60  		Description: user.Description,
    61  		// counter's
    62  		Followers:    user.NumFollowers,
    63  		Following:    user.NumFollowing,
    64  		StarredRepos: user.NumStars,
    65  	}
    66  
    67  	result.Visibility = user.Visibility.String()
    68  
    69  	// hide primary email if API caller is anonymous or user keep email private
    70  	if signed && (!user.KeepEmailPrivate || authed) {
    71  		result.Email = user.Email
    72  	}
    73  
    74  	// only site admin will get these information and possibly user himself
    75  	if authed {
    76  		result.IsAdmin = user.IsAdmin
    77  		result.LoginName = user.LoginName
    78  		result.LastLogin = user.LastLoginUnix.AsTime()
    79  		result.Language = user.Language
    80  		result.IsActive = user.IsActive
    81  		result.ProhibitLogin = user.ProhibitLogin
    82  	}
    83  	return result
    84  }
    85  
    86  // User2UserSettings return UserSettings based on a user
    87  func User2UserSettings(user *user_model.User) api.UserSettings {
    88  	return api.UserSettings{
    89  		FullName:      user.FullName,
    90  		Website:       user.Website,
    91  		Location:      user.Location,
    92  		Language:      user.Language,
    93  		Description:   user.Description,
    94  		Theme:         user.Theme,
    95  		HideEmail:     user.KeepEmailPrivate,
    96  		HideActivity:  user.KeepActivityPrivate,
    97  		DiffViewStyle: user.DiffViewStyle,
    98  	}
    99  }
   100  
   101  // ToUserAndPermission return User and its collaboration permission for a repository
   102  func ToUserAndPermission(ctx context.Context, user, doer *user_model.User, accessMode perm.AccessMode) api.RepoCollaboratorPermission {
   103  	return api.RepoCollaboratorPermission{
   104  		User:       ToUser(ctx, user, doer),
   105  		Permission: accessMode.String(),
   106  		RoleName:   accessMode.String(),
   107  	}
   108  }