2021-11-13 19:18:06 +00:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-10-31 15:08:57 +00:00
|
|
|
"xorm.io/builder"
|
2021-11-18 14:42:18 +00:00
|
|
|
"xorm.io/xorm"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2021-11-13 19:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s storage) PermFind(user *model.User, repo *model.Repo) (*model.Perm, error) {
|
2023-07-07 11:10:16 +00:00
|
|
|
perm := new(model.Perm)
|
|
|
|
return perm, wrapGet(s.engine.
|
2024-06-27 07:32:06 +00:00
|
|
|
Where(builder.Eq{"user_id": user.ID, "repo_id": repo.ID}).
|
2023-07-07 11:10:16 +00:00
|
|
|
Get(perm))
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) PermUpsert(perm *model.Perm) error {
|
|
|
|
sess := s.engine.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:42:18 +00:00
|
|
|
if err := s.permUpsert(sess, perm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) permUpsert(sess *xorm.Session, perm *model.Perm) error {
|
2022-09-05 15:08:51 +00:00
|
|
|
if perm.RepoID == 0 && perm.Repo == nil {
|
2021-11-18 14:42:18 +00:00
|
|
|
return fmt.Errorf("could not determine repo for permission: %v", perm)
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// lookup repo based on name or forge ID if possible
|
2022-09-05 15:08:51 +00:00
|
|
|
if perm.RepoID == 0 && perm.Repo != nil {
|
2022-11-15 14:01:23 +00:00
|
|
|
r, err := s.getRepoNameFallback(sess, perm.Repo.ForgeRemoteID, perm.Repo.FullName)
|
2021-12-01 13:22:06 +00:00
|
|
|
if err != nil {
|
2021-11-13 19:18:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
perm.RepoID = r.ID
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 11:10:16 +00:00
|
|
|
exist, err := sess.Where(userIDAndRepoIDCond(perm)).Exist(new(model.Perm))
|
2021-11-13 19:18:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exist {
|
2023-07-07 11:10:16 +00:00
|
|
|
_, err = sess.Where(userIDAndRepoIDCond(perm)).AllCols().Update(perm)
|
2021-11-13 19:18:06 +00:00
|
|
|
} else {
|
|
|
|
// only Insert set auto created ID back to object
|
|
|
|
_, err = sess.Insert(perm)
|
|
|
|
}
|
2021-11-18 14:42:18 +00:00
|
|
|
return err
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 15:08:57 +00:00
|
|
|
// userPushOrAdminCondition return condition where user must have push or admin rights
|
2024-05-13 20:58:21 +00:00
|
|
|
// if used make sure to have permission table ("perms") joined.
|
2022-10-31 15:08:57 +00:00
|
|
|
func userPushOrAdminCondition(userID int64) builder.Cond {
|
2024-06-27 07:32:06 +00:00
|
|
|
return builder.Eq{"perms.user_id": userID}.
|
|
|
|
And(builder.Eq{"perms.push": true}.
|
|
|
|
Or(builder.Eq{"perms.admin": true}))
|
2022-10-31 15:08:57 +00:00
|
|
|
}
|
2023-07-07 11:10:16 +00:00
|
|
|
|
|
|
|
func userIDAndRepoIDCond(perm *model.Perm) builder.Cond {
|
2024-06-27 07:32:06 +00:00
|
|
|
return builder.Eq{"user_id": perm.UserID, "repo_id": perm.RepoID}
|
2023-07-07 11:10:16 +00:00
|
|
|
}
|