-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
console_account.go
137 lines (116 loc) · 4.75 KB
/
console_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2018 The Nakama 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 server
import (
"context"
"database/sql"
"github.com/cockroachdb/cockroach-go/crdb"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/empty"
"github.com/heroiclabs/nakama/api"
"github.com/heroiclabs/nakama/console"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *ConsoleServer) DeleteAccount(ctx context.Context, in *console.AccountDeleteRequest) (*empty.Empty, error) {
userID := uuid.FromStringOrNil(in.Id)
if userID == uuid.Nil {
return nil, status.Error(codes.InvalidArgument, "Invalid user ID was provided.")
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
s.logger.Error("Could not begin database transaction.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to delete the user.")
}
if err := crdb.ExecuteInTx(ctx, tx, func() error {
count, err := DeleteUser(ctx, tx, userID)
if err != nil {
s.logger.Debug("Could not delete user", zap.Error(err), zap.String("user_id", in.Id))
return err
} else if count == 0 {
s.logger.Info("No user was found to delete. Skipping blacklist.", zap.String("user_id", in.Id))
return nil
}
err = LeaderboardRecordsDeleteAll(ctx, s.logger, tx, userID)
if err != nil {
s.logger.Debug("Could not delete leaderboard records.", zap.Error(err), zap.String("user_id", in.Id))
return err
}
err = GroupDeleteAll(ctx, s.logger, tx, userID)
if err != nil {
s.logger.Debug("Could not delete groups and relationships.", zap.Error(err), zap.String("user_id", in.Id))
return err
}
if in.RecordDeletion == nil || in.RecordDeletion.GetValue() {
return s.RecordAccountDeletion(ctx, tx, userID)
}
return nil
}); err != nil {
s.logger.Error("Error occurred while trying to delete the user.", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to delete the user.")
}
return &empty.Empty{}, nil
}
func (s *ConsoleServer) DeleteAccounts(ctx context.Context, in *empty.Empty) (*empty.Empty, error) {
query := "TRUNCATE TABLE users, leaderboard, groups CASCADE"
_, err := s.db.ExecContext(ctx, query)
if err != nil {
s.logger.Error("Error occurred while trying to deleting all users.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to delete all users.")
}
return &empty.Empty{}, nil
}
func (s *ConsoleServer) GetAccount(ctx context.Context, in *console.AccountIdRequest) (*api.Account, error) {
userID := uuid.FromStringOrNil(in.Id)
if userID == uuid.Nil {
return nil, status.Error(codes.InvalidArgument, "Invalid user ID was provided.")
}
// Core user account.
account, err := GetAccount(ctx, s.logger, s.db, nil, userID)
if err != nil {
if err == ErrAccountNotFound {
return nil, status.Error(codes.NotFound, "Account not found.")
}
s.logger.Error("Could not export account data", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
return account, nil
}
func (s *ConsoleServer) ListAccounts(ctx context.Context, in *empty.Empty) (*console.AccountList, error) {
rows, err := s.db.QueryContext(ctx, "SELECT id FROM users WHERE id != $1 ORDER BY update_time DESC LIMIT 100", uuid.Nil)
if err != nil {
s.logger.Error("Could not list users.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list users.")
}
userIDs := make([]string, 0)
for rows.Next() {
var userID sql.NullString
if rows.Scan(&userID); err != nil {
s.logger.Error("Could not list users.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list users.")
}
userIDs = append(userIDs, userID.String)
}
accounts := make([]*api.Account, 0)
for _, id := range userIDs {
account, err := GetAccount(ctx, s.logger, s.db, nil, uuid.Must(uuid.FromString(id)))
if err != nil {
s.logger.Error("Could not get user while listing.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list users.")
}
accounts = append(accounts, account)
}
return &console.AccountList{Accounts: accounts}, nil
}