This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
The following commit(s) were added to refs/heads/master by this push:
new 894f879 Fix: no create/update time in list account/role response body
(#1081)
894f879 is described below
commit 894f879eb63f39ec36ac3f5f8ec249f64e2b783c
Author: little-cui <[email protected]>
AuthorDate: Fri Jul 2 09:54:10 2021 +0800
Fix: no create/update time in list account/role response body (#1081)
* Fix: no create/update time in list account/role response body
* Fix: add some UTs
---
datasource/account_test.go | 26 ++++++++++++++++++++++++++
datasource/mongo/account.go | 10 +++++++++-
datasource/mongo/client/model/types.go | 2 ++
datasource/mongo/role.go | 5 +++++
datasource/mongo/util/db.go | 12 ++++++++++++
datasource/role_test.go | 18 +++++++++++++++++-
6 files changed, 71 insertions(+), 2 deletions(-)
diff --git a/datasource/account_test.go b/datasource/account_test.go
index 153e87d..9991a11 100644
--- a/datasource/account_test.go
+++ b/datasource/account_test.go
@@ -21,6 +21,7 @@ package datasource_test
import (
"context"
+ "strconv"
"testing"
"time"
@@ -92,6 +93,31 @@ func TestAccount(t *testing.T) {
_, err =
datasource.GetAccountManager().DeleteAccount(context.Background(),
[]string{a2.Name})
assert.NoError(t, err)
})
+ t.Run("add and update accounts, should have create/update time", func(t
*testing.T) {
+ err :=
datasource.GetAccountManager().CreateAccount(context.Background(), &a1)
+ assert.NoError(t, err)
+
+ r, err :=
datasource.GetAccountManager().GetAccount(context.Background(), a1.Name)
+ assert.NoError(t, err)
+ dt, _ := strconv.Atoi(r.CreateTime)
+ assert.Less(t, 0, dt)
+ assert.Equal(t, r.CreateTime, r.UpdateTime)
+
+ time.Sleep(time.Second)
+ a1.Password = "new-password"
+ err =
datasource.GetAccountManager().UpdateAccount(context.Background(), a1.Name, &a1)
+ assert.NoError(t, err)
+
+ old, _ := strconv.Atoi(r.UpdateTime)
+ r, err =
datasource.GetAccountManager().GetAccount(context.Background(), a1.Name)
+ assert.NoError(t, err)
+ last, _ := strconv.Atoi(r.UpdateTime)
+ assert.Less(t, old, last)
+ assert.NotEqual(t, r.CreateTime, r.UpdateTime)
+
+ _, err =
datasource.GetAccountManager().DeleteAccount(context.Background(),
[]string{a1.Name})
+ assert.NoError(t, err)
+ })
}
func TestAccountLock(t *testing.T) {
diff --git a/datasource/mongo/account.go b/datasource/mongo/account.go
index d2850a1..ecc2c3d 100644
--- a/datasource/mongo/account.go
+++ b/datasource/mongo/account.go
@@ -20,6 +20,8 @@ package mongo
import (
"context"
"fmt"
+ "strconv"
+ "time"
"github.com/go-chassis/cari/rbac"
"go.mongodb.org/mongo-driver/mongo"
@@ -52,7 +54,11 @@ func (ds *AccountManager) CreateAccount(ctx context.Context,
a *rbac.Account) er
log.Error(msg, err)
return err
}
+ a.Role = ""
+ a.CurrentPassword = ""
a.ID = util.GenerateUUID()
+ a.CreateTime = strconv.FormatInt(time.Now().Unix(), 10)
+ a.UpdateTime = a.CreateTime
_, err = client.GetMongoClient().Insert(ctx, model.CollectionAccount, a)
if err != nil {
if client.IsDuplicateKey(err) {
@@ -142,10 +148,12 @@ func (ds *AccountManager) UpdateAccount(ctx
context.Context, name string, accoun
filter := mutil.NewFilter(mutil.AccountName(name))
setFilter := mutil.NewFilter(
mutil.ID(account.ID),
- mutil.Password(account.Password), mutil.Roles(account.Roles),
+ mutil.Password(account.Password),
+ mutil.Roles(account.Roles),
mutil.TokenExpirationTime(account.TokenExpirationTime),
mutil.CurrentPassword(account.CurrentPassword),
mutil.Status(account.Status),
+ mutil.AccountUpdateTime(strconv.FormatInt(time.Now().Unix(),
10)),
)
updateFilter := mutil.NewFilter(mutil.Set(setFilter))
res, err := client.GetMongoClient().Update(ctx,
model.CollectionAccount, filter, updateFilter)
diff --git a/datasource/mongo/client/model/types.go
b/datasource/mongo/client/model/types.go
index 39ea6be..f92f3b2 100644
--- a/datasource/mongo/client/model/types.go
+++ b/datasource/mongo/client/model/types.go
@@ -70,6 +70,8 @@ const (
ColumnAccountName = "name"
ColumnRoleName = "name"
ColumnPerms = "perms"
+ ColumnAccountUpdateTime = "updatetime"
+ ColumnRoleUpdateTime = "updatetime"
ColumnPassword = "password"
ColumnRoles = "roles"
ColumnTokenExpirationTime = "token_expiration_time"
diff --git a/datasource/mongo/role.go b/datasource/mongo/role.go
index 7fead6b..fbbb4ef 100644
--- a/datasource/mongo/role.go
+++ b/datasource/mongo/role.go
@@ -19,6 +19,8 @@ package mongo
import (
"context"
+ "strconv"
+ "time"
"github.com/go-chassis/cari/rbac"
"go.mongodb.org/mongo-driver/bson"
@@ -44,6 +46,8 @@ func (ds *RoleManager) CreateRole(ctx context.Context, r
*rbac.Role) error {
return datasource.ErrRoleDuplicated
}
r.ID = util.GenerateUUID()
+ r.CreateTime = strconv.FormatInt(time.Now().Unix(), 10)
+ r.UpdateTime = r.CreateTime
_, err = client.GetMongoClient().Insert(ctx, model.CollectionRole, r)
if err != nil {
if client.IsDuplicateKey(err) {
@@ -130,6 +134,7 @@ func (ds *RoleManager) UpdateRole(ctx context.Context, name
string, role *rbac.R
mutil.ID(role.ID),
mutil.RoleName(role.Name),
mutil.Perms(role.Perms),
+ mutil.RoleUpdateTime(strconv.FormatInt(time.Now().Unix(), 10)),
)
updateFilter := mutil.NewFilter(mutil.Set(setFilter))
_, err := client.GetMongoClient().Update(ctx, model.CollectionRole,
filter, updateFilter)
diff --git a/datasource/mongo/util/db.go b/datasource/mongo/util/db.go
index a2dd0bf..0ebb40f 100644
--- a/datasource/mongo/util/db.go
+++ b/datasource/mongo/util/db.go
@@ -98,6 +98,18 @@ func Perms(perms []*rbac.Permission) Option {
}
}
+func AccountUpdateTime(dt interface{}) Option {
+ return func(filter bson.M) {
+ filter[model.ColumnAccountUpdateTime] = dt
+ }
+}
+
+func RoleUpdateTime(dt interface{}) Option {
+ return func(filter bson.M) {
+ filter[model.ColumnRoleUpdateTime] = dt
+ }
+}
+
func AccountLockKey(key interface{}) Option {
return func(filter bson.M) {
filter[model.ColumnAccountLockKey] = key
diff --git a/datasource/role_test.go b/datasource/role_test.go
index 9010b42..9eaafc2 100644
--- a/datasource/role_test.go
+++ b/datasource/role_test.go
@@ -19,7 +19,9 @@ package datasource_test
import (
"context"
+ "strconv"
"testing"
+ "time"
"github.com/go-chassis/cari/rbac"
@@ -55,6 +57,9 @@ func TestRole(t *testing.T) {
r, err :=
datasource.GetRoleManager().GetRole(context.Background(), "test-role1")
assert.NoError(t, err)
assert.Equal(t, r1, *r)
+ dt, _ := strconv.Atoi(r.CreateTime)
+ assert.Less(t, 0, dt)
+ assert.Equal(t, r.CreateTime, r.UpdateTime)
})
t.Run("role should exist", func(t *testing.T) {
exist, err :=
datasource.GetRoleManager().RoleExist(context.Background(), "test-role1")
@@ -68,9 +73,20 @@ func TestRole(t *testing.T) {
})
t.Run("update role should success", func(t *testing.T) {
+ r, err :=
datasource.GetRoleManager().GetRole(context.Background(), "test-role1")
+ assert.NoError(t, err)
+ old, _ := strconv.Atoi(r.UpdateTime)
+
+ time.Sleep(time.Second)
r1.ID = "11111-22222-33333-4"
- err :=
datasource.GetRoleManager().UpdateRole(context.Background(), "test-role1", &r1)
+ err =
datasource.GetRoleManager().UpdateRole(context.Background(), "test-role1", &r1)
+ assert.NoError(t, err)
+
+ r, err =
datasource.GetRoleManager().GetRole(context.Background(), "test-role1")
assert.NoError(t, err)
+ last, _ := strconv.Atoi(r.UpdateTime)
+ assert.Less(t, old, last)
+ assert.NotEqual(t, r.CreateTime, r.UpdateTime)
})
t.Run("add new role should success", func(t *testing.T) {
err :=
datasource.GetRoleManager().CreateRole(context.Background(), &r2)