aboutsummaryrefslogtreecommitdiff
path: root/internal/userutil
diff options
context:
space:
mode:
authorJoe Chen <jc@unknwon.io>2022-10-22 14:41:40 +0800
committerGitHub <noreply@github.com>2022-10-22 14:41:40 +0800
commitc502dc6ed888a4cf2c8b36176585f4166536ab6d (patch)
tree71e3f758069dc2435eecb88ae786b2efaaa972f9 /internal/userutil
parent260e990be75885e1a560df449dacdeecafeffdcf (diff)
refactor(db): move some methods from `user.go` to `users.go` (#7195)
Diffstat (limited to 'internal/userutil')
-rw-r--r--internal/userutil/userutil.go36
-rw-r--r--internal/userutil/userutil_test.go40
2 files changed, 76 insertions, 0 deletions
diff --git a/internal/userutil/userutil.go b/internal/userutil/userutil.go
new file mode 100644
index 00000000..87b8f15b
--- /dev/null
+++ b/internal/userutil/userutil.go
@@ -0,0 +1,36 @@
+// Copyright 2022 The Gogs 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 userutil
+
+import (
+ "encoding/hex"
+ "fmt"
+ "strings"
+
+ "gogs.io/gogs/internal/conf"
+ "gogs.io/gogs/internal/tool"
+)
+
+// DashboardURLPath returns the URL path to the user or organization dashboard.
+func DashboardURLPath(name string, isOrganization bool) string {
+ if isOrganization {
+ return conf.Server.Subpath + "/org/" + name + "/dashboard/"
+ }
+ return conf.Server.Subpath + "/"
+}
+
+// GenerateActivateCode generates an activate code based on user information and
+// the given email.
+func GenerateActivateCode(id int64, email, name, password, rands string) string {
+ code := tool.CreateTimeLimitCode(
+ fmt.Sprintf("%d%s%s%s%s", id, email, strings.ToLower(name), password, rands),
+ conf.Auth.ActivateCodeLives,
+ nil,
+ )
+
+ // Add tailing hex username
+ code += hex.EncodeToString([]byte(strings.ToLower(name)))
+ return code
+}
diff --git a/internal/userutil/userutil_test.go b/internal/userutil/userutil_test.go
new file mode 100644
index 00000000..a62363a5
--- /dev/null
+++ b/internal/userutil/userutil_test.go
@@ -0,0 +1,40 @@
+// Copyright 2022 The Gogs 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 userutil
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+
+ "gogs.io/gogs/internal/conf"
+ "gogs.io/gogs/internal/tool"
+)
+
+func TestDashboardURLPath(t *testing.T) {
+ t.Run("user", func(t *testing.T) {
+ got := DashboardURLPath("alice", false)
+ want := "/"
+ assert.Equal(t, want, got)
+ })
+
+ t.Run("organization", func(t *testing.T) {
+ got := DashboardURLPath("acme", true)
+ want := "/org/acme/dashboard/"
+ assert.Equal(t, want, got)
+ })
+}
+
+func TestGenerateActivateCode(t *testing.T) {
+ conf.SetMockAuth(t,
+ conf.AuthOpts{
+ ActivateCodeLives: 10,
+ },
+ )
+
+ code := GenerateActivateCode(1, "alice@example.com", "Alice", "123456", "rands")
+ got := tool.VerifyTimeLimitCode("1alice@example.comalice123456rands", conf.Auth.ActivateCodeLives, code[:tool.TIME_LIMIT_CODE_LENGTH])
+ assert.True(t, got)
+}