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 (
|
2023-05-11 03:19:35 +00:00
|
|
|
"xorm.io/xorm"
|
2023-09-02 11:31:10 +00:00
|
|
|
|
2023-11-07 07:04:33 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/server/model"
|
2021-11-13 19:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s storage) GetUser(id int64) (*model.User, error) {
|
|
|
|
user := new(model.User)
|
|
|
|
return user, wrapGet(s.engine.ID(id).Get(user))
|
|
|
|
}
|
|
|
|
|
2023-05-11 03:19:35 +00:00
|
|
|
func (s storage) GetUserRemoteID(remoteID model.ForgeRemoteID, login string) (*model.User, error) {
|
|
|
|
sess := s.engine.NewSession()
|
|
|
|
user := new(model.User)
|
|
|
|
err := wrapGet(sess.Where("forge_remote_id = ?", remoteID).Get(user))
|
|
|
|
if err != nil {
|
|
|
|
user, err = s.getUserLogin(sess, login)
|
|
|
|
}
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
func (s storage) GetUserLogin(login string) (*model.User, error) {
|
2023-05-11 03:19:35 +00:00
|
|
|
return s.getUserLogin(s.engine.NewSession(), login)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) getUserLogin(sess *xorm.Session, login string) (*model.User, error) {
|
2021-11-13 19:18:06 +00:00
|
|
|
user := new(model.User)
|
2023-05-11 03:19:35 +00:00
|
|
|
return user, wrapGet(sess.Where("user_login=?", login).Get(user))
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
func (s storage) GetUserList(p *model.ListOptions) ([]*model.User, error) {
|
|
|
|
var users []*model.User
|
|
|
|
return users, s.paginate(p).OrderBy("user_id").Find(&users)
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) GetUserCount() (int64, error) {
|
|
|
|
return s.engine.Count(new(model.User))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) CreateUser(user *model.User) error {
|
2023-08-21 13:04:12 +00:00
|
|
|
sess := s.engine.NewSession()
|
|
|
|
org := &model.Org{
|
|
|
|
Name: user.Login,
|
|
|
|
IsUser: true,
|
|
|
|
}
|
|
|
|
err := s.orgCreate(org, sess)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.OrgID = org.ID
|
2021-11-13 19:18:06 +00:00
|
|
|
// only Insert set auto created ID back to object
|
2023-08-21 13:04:12 +00:00
|
|
|
_, err = sess.Insert(user)
|
2021-11-13 19:18:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) UpdateUser(user *model.User) error {
|
|
|
|
_, err := s.engine.ID(user.ID).AllCols().Update(user)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) DeleteUser(user *model.User) error {
|
2022-10-31 15:08:57 +00:00
|
|
|
sess := s.engine.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-02 13:00:13 +00:00
|
|
|
if err := s.orgDelete(sess, user.OrgID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-15 13:22:39 +00:00
|
|
|
if err := wrapDelete(sess.ID(user.ID).Delete(new(model.User))); err != nil {
|
2022-10-31 15:08:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Where("perm_user_id = ?", user.ID).Delete(new(model.Perm)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|