mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	add publickey & access
This commit is contained in:
		
							
								
								
									
										29
									
								
								models/access.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								models/access.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						Readable = iota + 1
 | 
				
			||||||
 | 
						Writable
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Access struct {
 | 
				
			||||||
 | 
						Id       int64
 | 
				
			||||||
 | 
						UserName string    `xorm:"unique(s)"`
 | 
				
			||||||
 | 
						RepoName string    `xorm:"unique(s)"`
 | 
				
			||||||
 | 
						Mode     int       `xorm:"unique(s)"`
 | 
				
			||||||
 | 
						Created  time.Time `xorm:"created"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func AddAccess(access *Access) error {
 | 
				
			||||||
 | 
						_, err := orm.Insert(access)
 | 
				
			||||||
 | 
						return err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// if one user can read or write one repository
 | 
				
			||||||
 | 
					func HasAccess(userName, repoName, mode string) (bool, error) {
 | 
				
			||||||
 | 
						return orm.Get(&Access{0, strings.ToLower(userName), strings.ToLower(repoName), mode})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -4,25 +4,13 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import "github.com/lunny/xorm"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/lunny/xorm"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	orm          *xorm.Engine
 | 
						orm          *xorm.Engine
 | 
				
			||||||
	repoRootPath string
 | 
						repoRootPath string
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type PublicKey struct {
 | 
					 | 
				
			||||||
	Id      int64
 | 
					 | 
				
			||||||
	Name    string    `xorm:"unique not null"`
 | 
					 | 
				
			||||||
	Content string    `xorm:"text not null"`
 | 
					 | 
				
			||||||
	Created time.Time `xorm:"created"`
 | 
					 | 
				
			||||||
	Updated time.Time `xorm:"updated"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Members struct {
 | 
					type Members struct {
 | 
				
			||||||
	Id     int64
 | 
						Id     int64
 | 
				
			||||||
	OrgId  int64 `xorm:"unique(s) index"`
 | 
						OrgId  int64 `xorm:"unique(s) index"`
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										55
									
								
								models/publickey.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								models/publickey.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
				
			|||||||
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						publicKeyRootPath string
 | 
				
			||||||
 | 
						tmplPublicKey     = "### autogenerated by gitgos, DO NOT EDIT\n" +
 | 
				
			||||||
 | 
							"command=\"gitosis-serve %s\",no-port-forwarding," +
 | 
				
			||||||
 | 
							"no-X11-forwarding,no-agent-forwarding,no-pty %s"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type PublicKey struct {
 | 
				
			||||||
 | 
						Id      int64
 | 
				
			||||||
 | 
						OwnerId int64     `xorm:"index"`
 | 
				
			||||||
 | 
						Name    string    `xorm:"unique not null"`
 | 
				
			||||||
 | 
						Content string    `xorm:"text not null"`
 | 
				
			||||||
 | 
						Created time.Time `xorm:"created"`
 | 
				
			||||||
 | 
						Updated time.Time `xorm:"updated"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GenAuthorizedKey(user, key string) string {
 | 
				
			||||||
 | 
						return fmt.Sprintf(tmplPublicKey, user, key)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func AddPublicKey(key *PublicKey, user string) error {
 | 
				
			||||||
 | 
						_, err := orm.Insert(key)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = SaveAuthorizedKeyFile(user, key.Content)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							_, err2 := orm.Delete(key)
 | 
				
			||||||
 | 
							if err2 != nil {
 | 
				
			||||||
 | 
								// TODO: logo the error
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func SaveAuthorizedKeyFile(user, key string) error {
 | 
				
			||||||
 | 
						f, err := os.Create(filepath.Join(publicKeyRootPath, user+".pub"))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						_, err = f.WriteString(GenAuthorizedKey(user, key))
 | 
				
			||||||
 | 
						return err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -39,7 +39,7 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
 | 
				
			|||||||
func CreateRepository(user *User, reposName string) (*Repo, error) {
 | 
					func CreateRepository(user *User, reposName string) (*Repo, error) {
 | 
				
			||||||
	p := filepath.Join(repoRootPath, user.Name)
 | 
						p := filepath.Join(repoRootPath, user.Name)
 | 
				
			||||||
	os.MkdirAll(p, os.ModePerm)
 | 
						os.MkdirAll(p, os.ModePerm)
 | 
				
			||||||
	f := filepath.Join(p, reposName)
 | 
						f := filepath.Join(p, reposName+".git")
 | 
				
			||||||
	_, err := git.InitRepository(f, false)
 | 
						_, err := git.InitRepository(f, false)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
@@ -108,7 +108,7 @@ func DeleteRepository(user *User, reposName string) (err error) {
 | 
				
			|||||||
		session.Rollback()
 | 
							session.Rollback()
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err = os.RemoveAll(filepath.Join(repoRootPath, user.Name, reposName)); err != nil {
 | 
						if err = os.RemoveAll(filepath.Join(repoRootPath, user.Name, reposName+".git")); err != nil {
 | 
				
			||||||
		// TODO: log and delete manully
 | 
							// TODO: log and delete manully
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user