add models and services
This commit is contained in:
54
models/auth/secret.go
Normal file
54
models/auth/secret.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
type ErrSecretNameInvalid struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (err ErrSecretNameInvalid) Error() string {
|
||||
return fmt.Sprintf("secret name %s is invalid", err.Name)
|
||||
}
|
||||
|
||||
type ErrSecretDataInvalid struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
func (err ErrSecretDataInvalid) Error() string {
|
||||
return fmt.Sprintf("secret data %s is invalid", err.Data)
|
||||
}
|
||||
|
||||
var nameRE = regexp.MustCompile("[^a-zA-Z0-9-_.]+")
|
||||
|
||||
type Secret struct {
|
||||
ID int64
|
||||
UserID int64 `xorm:"index"`
|
||||
RepoID int64 `xorm:"index"`
|
||||
Name string
|
||||
Data string
|
||||
PullRequest bool
|
||||
CreatedUnix timeutil.TimeStamp
|
||||
}
|
||||
|
||||
// Validate validates the required fields and formats.
|
||||
func (s *Secret) Validate() error {
|
||||
switch {
|
||||
case len(s.Name) == 0:
|
||||
return ErrSecretNameInvalid{Name: s.Name}
|
||||
case len(s.Data) == 0:
|
||||
return ErrSecretDataInvalid{Data: s.Data}
|
||||
case nameRE.MatchString(s.Name):
|
||||
return ErrSecretNameInvalid{Name: s.Name}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,12 @@
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
// SearchOrderBy is used to sort the result
|
||||
type SearchOrderBy string
|
||||
|
||||
@@ -28,3 +34,14 @@ const (
|
||||
SearchOrderByForks SearchOrderBy = "num_forks ASC"
|
||||
SearchOrderByForksReverse SearchOrderBy = "num_forks DESC"
|
||||
)
|
||||
|
||||
func FindObjects[Object any](ctx context.Context, cond builder.Cond, opts *ListOptions, objects *[]*Object) error {
|
||||
sess := GetEngine(ctx).Where(cond)
|
||||
if opts != nil && opts.PageSize > 0 {
|
||||
if opts.Page < 1 {
|
||||
opts.Page = 1
|
||||
}
|
||||
sess.Limit(opts.PageSize, opts.PageSize * (opts.Page - 1))
|
||||
}
|
||||
return sess.Find(objects)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user