diff options
author | Lanre Adelowo <adelowomailbox@gmail.com> | 2019-01-09 18:22:57 +0100 |
---|---|---|
committer | techknowlogick <hello@techknowlogick.com> | 2019-01-09 12:22:57 -0500 |
commit | 8d2c24f7f9b9bce3a806e4748623bd3b2742025b (patch) | |
tree | 2010e6ffaf542d0828c496c31afa56f816c069d5 /models | |
parent | ea518681d95c9ef8ae5ed71d6d8cd7cfb6994a50 (diff) | |
download | gitea-8d2c24f7f9b9bce3a806e4748623bd3b2742025b.tar.gz gitea-8d2c24f7f9b9bce3a806e4748623bd3b2742025b.zip |
Allow for user specific themes (#5668)
* add migration and basic UI for changing a user's theme
* update user themem
* use right text on button
* load theme based on users' selection
* load theme based on users' selection in pwa too
* update sample config
* delete older theme loading
* implement AfterLoad to set users' theme properly
* set up default theme when creating a user. This uses the installation wide theme
* use flash messages for error
* set default theme when creating a user from the cli
* fix @lunny review
Diffstat (limited to 'models')
-rw-r--r-- | models/migrations/migrations.go | 4 | ||||
-rw-r--r-- | models/migrations/v77.go | 17 | ||||
-rw-r--r-- | models/user.go | 15 |
3 files changed, 35 insertions, 1 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 1fa94c42aa..4c6b6d64bc 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -18,7 +18,7 @@ import ( "github.com/Unknwon/com" "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" - "gopkg.in/ini.v1" + ini "gopkg.in/ini.v1" "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/log" @@ -206,6 +206,8 @@ var migrations = []Migration{ NewMigration("clear nonused data which not deleted when user was deleted", clearNonusedData), // v76 -> v77 NewMigration("add pull request rebase with merge commit", addPullRequestRebaseWithMerge), + // v77 -> v78 + NewMigration("add theme to users", addUserDefaultTheme), } // Migrate database to current version diff --git a/models/migrations/v77.go b/models/migrations/v77.go new file mode 100644 index 0000000000..12e7456642 --- /dev/null +++ b/models/migrations/v77.go @@ -0,0 +1,17 @@ +// Copyright 2019 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 migrations + +import ( + "github.com/go-xorm/xorm" +) + +func addUserDefaultTheme(x *xorm.Engine) error { + type User struct { + Theme string `xorm:"VARCHAR(30)"` + } + + return x.Sync2(new(User)) +} diff --git a/models/user.go b/models/user.go index 3a47346e34..764c2280d7 100644 --- a/models/user.go +++ b/models/user.go @@ -140,6 +140,7 @@ type User struct { // Preferences DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"` + Theme string `xorm:"NOT NULL DEFAULT ''"` } // BeforeUpdate is invoked from XORM before updating this object. @@ -165,6 +166,13 @@ func (u *User) BeforeUpdate() { u.Description = base.TruncateString(u.Description, 255) } +// AfterLoad is invoked from XORM after filling all the fields of this object. +func (u *User) AfterLoad() { + if u.Theme == "" { + u.Theme = setting.UI.DefaultTheme + } +} + // SetLastLogin set time to last login func (u *User) SetLastLogin() { u.LastLoginUnix = util.TimeStampNow() @@ -176,6 +184,12 @@ func (u *User) UpdateDiffViewStyle(style string) error { return UpdateUserCols(u, "diff_view_style") } +// UpdateTheme updates a users' theme irrespective of the site wide theme +func (u *User) UpdateTheme(themeName string) error { + u.Theme = themeName + return UpdateUserCols(u, "theme") +} + // getEmail returns an noreply email, if the user has set to keep his // email address private, otherwise the primary email address. func (u *User) getEmail() string { @@ -777,6 +791,7 @@ func CreateUser(u *User) (err error) { u.HashPassword(u.Passwd) u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization u.MaxRepoCreation = -1 + u.Theme = setting.UI.DefaultTheme if _, err = sess.Insert(u); err != nil { return err |