forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
55 lines (48 loc) · 1.35 KB
/
user.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
package pwd
import (
"errors"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/storage"
)
var userBannedError = errors.New("User is banned")
func (p *pwd) UserNewLoginRequest(providerName string) (*types.LoginRequest, error) {
req := &types.LoginRequest{Id: p.generator.NewId(), Provider: providerName}
if err := p.storage.LoginRequestPut(req); err != nil {
return nil, err
}
return req, nil
}
func (p *pwd) UserGetLoginRequest(id string) (*types.LoginRequest, error) {
if req, err := p.storage.LoginRequestGet(id); err != nil {
return nil, err
} else {
return req, nil
}
}
func (p *pwd) UserLogin(loginRequest *types.LoginRequest, user *types.User) (*types.User, error) {
if err := p.storage.LoginRequestDelete(loginRequest.Id); err != nil {
return nil, err
}
u, err := p.storage.UserFindByProvider(user.Provider, user.ProviderUserId)
if err != nil {
if storage.NotFound(err) {
user.Id = p.generator.NewId()
if err := p.storage.UserPut(user); err != nil {
return nil, err
}
return user, nil
}
return nil, err
}
return u, nil
}
func (p *pwd) UserGet(id string) (*types.User, error) {
var user *types.User
var err error
if user, err = p.storage.UserGet(id); err != nil {
return nil, err
} else if user.IsBanned {
return user, userBannedError
}
return user, nil
}