2021-11-13 21:18:06 +02:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// 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
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// 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.
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
package model
|
|
|
|
|
2017-07-17 06:01:35 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// validate a username (e.g. from github)
|
2017-10-05 23:02:23 +02:00
|
|
|
var reUsername = regexp.MustCompile("^[a-zA-Z0-9-_.]+$")
|
2017-07-17 06:01:35 +02:00
|
|
|
|
|
|
|
var errUserLoginInvalid = errors.New("Invalid User Login")
|
|
|
|
|
2016-03-30 22:15:28 +02:00
|
|
|
// User represents a registered user.
|
|
|
|
//
|
|
|
|
// swagger:model user
|
2015-04-07 10:20:55 +02:00
|
|
|
type User struct {
|
2016-03-30 22:15:28 +02:00
|
|
|
// the id for this user.
|
|
|
|
//
|
|
|
|
// required: true
|
2021-11-13 21:18:06 +02:00
|
|
|
ID int64 `json:"id" xorm:"pk autoincr 'user_id'"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
|
|
|
// Login is the username for this user.
|
|
|
|
//
|
|
|
|
// required: true
|
2021-11-13 21:18:06 +02:00
|
|
|
Login string `json:"login" xorm:"UNIQUE 'user_login'"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
|
|
|
// Token is the oauth2 token.
|
2021-11-13 21:18:06 +02:00
|
|
|
Token string `json:"-" xorm:"TEXT 'user_token'"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
|
|
|
// Secret is the oauth2 token secret.
|
2021-11-13 21:18:06 +02:00
|
|
|
Secret string `json:"-" xorm:"TEXT 'user_secret'"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
2018-02-15 10:39:59 +02:00
|
|
|
// Expiry is the token and secret expiration timestamp.
|
2021-11-13 21:18:06 +02:00
|
|
|
Expiry int64 `json:"-" xorm:"user_expiry"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
|
|
|
// Email is the email address for this user.
|
|
|
|
//
|
|
|
|
// required: true
|
2021-11-13 21:18:06 +02:00
|
|
|
Email string `json:"email" xorm:" varchar(500) 'user_email'"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
|
|
|
// the avatar url for this user.
|
2021-11-13 21:18:06 +02:00
|
|
|
Avatar string `json:"avatar_url" xorm:" varchar(500) 'user_avatar'"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
|
|
|
// Activate indicates the user is active in the system.
|
2021-11-13 21:18:06 +02:00
|
|
|
Active bool `json:"active" xorm:"user_active"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
2017-07-14 21:58:38 +02:00
|
|
|
// Synced is the timestamp when the user was synced with the remote system.
|
2021-11-13 21:18:06 +02:00
|
|
|
Synced int64 `json:"synced" xorm:"user_synced"`
|
2017-07-14 21:58:38 +02:00
|
|
|
|
2016-03-30 22:15:28 +02:00
|
|
|
// Admin indicates the user is a system administrator.
|
2016-05-02 21:21:25 +02:00
|
|
|
//
|
Clean up config environment variables for server and agent (#218)
The goal here is to make consistent use of configuration environment variables prefixed `WOODPECKER_`. Where several variants existed, this PR aims to remove all but one option, leaving the most explicit.
This PR only changes server and agent code, but not documentation, in order to keep the PR digestible. Once we have consensus that this is correct, I'll change docs accordingly.
User (rather: admin) facing changes in this PR:
- In general, support for all server and agent config environment variables (env vars) starting with `DRONE_` is removed. The according `WOODPECKER_*` variables must be used instead.
- The env var `WOODPECKER_HOST` replaces `DRONE_HOST`, and `DRONE_SERVER_HOST`.
- The env var `WOODPECKER_AGENT_SECRET` is used to configure the shared secret which agents use to authenticate against the server. It replaces `WOODPECKER_SECRET`, `DRONE_SECRET`, `WOODPECKER_PASSWORD`, `DRONE_PASSWORD`, and `DRONE_AGENT_SECRET`.
- The env var `WOODPECKER_DATABASE_DRIVER` replaces `DRONE_DATABASE_DRIVER` and `DATABASE_DRIVER`.
- The env var `WOODPECKER_DATABASE_DATASOURCE` replaces `DRONE_DATABASE_DATASOURCE` and `DATABASE_CONFIG`.
2021-09-28 15:43:44 +02:00
|
|
|
// NOTE: This is sourced from the WOODPECKER_ADMINS environment variable and is no
|
2016-05-02 21:21:25 +02:00
|
|
|
// longer persisted in the database.
|
2021-11-13 21:18:06 +02:00
|
|
|
Admin bool `json:"admin,omitempty" xorm:"-"`
|
2016-03-30 22:15:28 +02:00
|
|
|
|
|
|
|
// Hash is a unique token used to sign tokens.
|
2021-11-13 21:18:06 +02:00
|
|
|
Hash string `json:"-" xorm:"UNIQUE varchar(500) 'user_hash'"`
|
2016-05-02 21:21:25 +02:00
|
|
|
|
|
|
|
// DEPRECATED Admin indicates the user is a system administrator.
|
2021-11-13 21:18:06 +02:00
|
|
|
XAdmin bool `json:"-" xorm:"user_admin"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName return database table name for xorm
|
|
|
|
func (User) TableName() string {
|
|
|
|
return "users"
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-07-17 06:01:35 +02:00
|
|
|
|
|
|
|
// Validate validates the required fields and formats.
|
|
|
|
func (u *User) Validate() error {
|
|
|
|
switch {
|
|
|
|
case len(u.Login) == 0:
|
|
|
|
return errUserLoginInvalid
|
|
|
|
case len(u.Login) > 250:
|
|
|
|
return errUserLoginInvalid
|
|
|
|
case !reUsername.MatchString(u.Login):
|
|
|
|
return errUserLoginInvalid
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|