mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-07 09:57:07 +02:00
removed duplication
This commit is contained in:
1 parent
959b903193
commit
3c0e3cf410
5 files changed
+14
-39
No files matched your search
+9
-34
@@ -20,48 +20,23 @@ func IsSSHURL(remote string) bool {
|
|||||||
return err == nil && u.Scheme == "ssh"
|
return err == nil && u.Scheme == "ssh"
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOrCreateSSHKeypairForUser gets or creates an SSH keypair for the given user
|
// GetOrCreateSSHKeypair gets or creates the managed SSH keypair for the given
|
||||||
func GetOrCreateSSHKeypairForUser(ctx context.Context, userID int64) (*user_model.UserSSHKeypair, error) {
|
// owner (user or organization — they share the same backing storage).
|
||||||
keypair, err := user_model.GetUserSSHKeypairByOwner(ctx, userID)
|
func GetOrCreateSSHKeypair(ctx context.Context, ownerID int64) (*user_model.UserSSHKeypair, error) {
|
||||||
|
keypair, err := user_model.GetUserSSHKeypairByOwner(ctx, ownerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsErrNotExist(err) {
|
if db.IsErrNotExist(err) {
|
||||||
log.Debug("Creating new SSH keypair for user %d", userID)
|
log.Debug("Creating new SSH keypair for owner %d", ownerID)
|
||||||
return user_model.CreateUserSSHKeypair(ctx, userID)
|
return user_model.CreateUserSSHKeypair(ctx, ownerID)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("failed to get SSH keypair for user %d: %w", userID, err)
|
return nil, fmt.Errorf("failed to get SSH keypair for owner %d: %w", ownerID, err)
|
||||||
}
|
}
|
||||||
return keypair, nil
|
return keypair, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOrCreateSSHKeypairForOrg gets or creates an SSH keypair for the given organization
|
// GetSSHKeypairForRepository gets the managed SSH keypair for the repository's owner.
|
||||||
func GetOrCreateSSHKeypairForOrg(ctx context.Context, orgID int64) (*user_model.UserSSHKeypair, error) {
|
|
||||||
keypair, err := user_model.GetUserSSHKeypairByOwner(ctx, orgID)
|
|
||||||
if err != nil {
|
|
||||||
if db.IsErrNotExist(err) {
|
|
||||||
log.Debug("Creating new SSH keypair for organization %d", orgID)
|
|
||||||
return user_model.CreateUserSSHKeypair(ctx, orgID)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("failed to get SSH keypair for organization %d: %w", orgID, err)
|
|
||||||
}
|
|
||||||
return keypair, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSSHKeypairForRepository gets the appropriate SSH keypair for a repository
|
|
||||||
// If the repository belongs to an organization, it uses the org's keypair,
|
|
||||||
// otherwise it uses the user's keypair
|
|
||||||
func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository) (*user_model.UserSSHKeypair, error) {
|
func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository) (*user_model.UserSSHKeypair, error) {
|
||||||
if repo.Owner == nil {
|
return GetOrCreateSSHKeypair(ctx, repo.OwnerID)
|
||||||
owner, err := user_model.GetUserByID(ctx, repo.OwnerID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get repository owner: %w", err)
|
|
||||||
}
|
|
||||||
repo.Owner = owner
|
|
||||||
}
|
|
||||||
|
|
||||||
if repo.Owner.IsOrganization() {
|
|
||||||
return GetOrCreateSSHKeypairForOrg(ctx, repo.OwnerID)
|
|
||||||
}
|
|
||||||
return GetOrCreateSSHKeypairForUser(ctx, repo.OwnerID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupManagedSSHAgent prepares SSH key-based authentication for a mirror or
|
// SetupManagedSSHAgent prepares SSH key-based authentication for a mirror or
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func GetManagedSSHKey(ctx *context.APIContext) {
|
|||||||
// "404":
|
// "404":
|
||||||
// "$ref": "#/responses/notFound"
|
// "$ref": "#/responses/notFound"
|
||||||
|
|
||||||
keypair, err := ssh_module.GetOrCreateSSHKeypairForOrg(ctx, ctx.Org.Organization.ID)
|
keypair, err := ssh_module.GetOrCreateSSHKeypair(ctx, ctx.Org.Organization.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsErrNotExist(err) {
|
if db.IsErrNotExist(err) {
|
||||||
ctx.APIError(http.StatusNotFound, "SSH keypair not found")
|
ctx.APIError(http.StatusNotFound, "SSH keypair not found")
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func GetManagedSSHKey(ctx *context.APIContext) {
|
|||||||
// "404":
|
// "404":
|
||||||
// "$ref": "#/responses/notFound"
|
// "$ref": "#/responses/notFound"
|
||||||
|
|
||||||
keypair, err := ssh_module.GetOrCreateSSHKeypairForUser(ctx, ctx.Doer.ID)
|
keypair, err := ssh_module.GetOrCreateSSHKeypair(ctx, ctx.Doer.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsErrNotExist(err) {
|
if db.IsErrNotExist(err) {
|
||||||
ctx.APIError(http.StatusNotFound, "SSH keypair not found")
|
ctx.APIError(http.StatusNotFound, "SSH keypair not found")
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ func SSHKeys(ctx *context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
keypair, err := ssh_module.GetOrCreateSSHKeypairForOrg(ctx, ctx.Org.Organization.ID)
|
keypair, err := ssh_module.GetOrCreateSSHKeypair(ctx, ctx.Org.Organization.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("GetOrCreateSSHKeypairForOrg", err)
|
ctx.ServerError("GetOrCreateSSHKeypair", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -343,7 +343,7 @@ func loadKeysData(ctx *context.Context) {
|
|||||||
ctx.Data["UserDisabledFeatures"] = user_model.DisabledFeaturesWithLoginType(ctx.Doer)
|
ctx.Data["UserDisabledFeatures"] = user_model.DisabledFeaturesWithLoginType(ctx.Doer)
|
||||||
|
|
||||||
// Load SSH mirror keypair if it exists
|
// Load SSH mirror keypair if it exists
|
||||||
mirrorKeypair, err := ssh_module.GetOrCreateSSHKeypairForUser(ctx, ctx.Doer.ID)
|
mirrorKeypair, err := ssh_module.GetOrCreateSSHKeypair(ctx, ctx.Doer.ID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ctx.Data["HasManagedSSHKey"] = true
|
ctx.Data["HasManagedSSHKey"] = true
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user