forked from admpub/nging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpUser.go
106 lines (91 loc) · 2.59 KB
/
ftpUser.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
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package model
import (
"errors"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/model/base"
)
type FtpUserAndGroup struct {
*dbschema.NgingFtpUser
Group *dbschema.NgingFtpUserGroup `db:"-,relation=id:group_id|gtZero"`
}
var DefaultSalt = ``
func NewFtpUser(ctx echo.Context) *FtpUser {
return &FtpUser{
NgingFtpUser: &dbschema.NgingFtpUser{},
Base: base.New(ctx),
}
}
type FtpUser struct {
*dbschema.NgingFtpUser
*base.Base
}
func (f *FtpUser) Exists(username string) (bool, error) {
return f.NgingFtpUser.Exists(nil, db.Cond{`username`: username})
}
func (f *FtpUser) CheckPasswd(username string, password string) (bool, error) {
exists, err := f.NgingFtpUser.Exists(nil, db.Cond{
`username`: username,
`password`: com.MakePassword(password, DefaultSalt),
})
if err != nil {
return exists, err
}
if exists {
_, err = f.RootPath(username)
if err != nil {
exists = false
}
}
return exists, err
}
var (
ErrNoneFtpDirectory = errors.New(`No accessible directory`)
ErrBannedFtpUser = errors.New(`The current account has been disabled`)
)
func (f *FtpUser) RootPath(username string) (basePath string, err error) {
err = f.Get(nil, db.Cond{`username`: username})
if err != nil {
return
}
if f.NgingFtpUser.GroupId > 0 {
m := NewFtpUserGroup(f.Base.Context)
err = m.Get(nil, db.Cond{`id`: f.NgingFtpUser.GroupId})
if err != nil {
return
}
if m.NgingFtpUserGroup.Banned == `Y` {
err = ErrBannedFtpUser
return
}
basePath = m.NgingFtpUserGroup.Directory
}
if f.NgingFtpUser.Banned == `Y` {
err = ErrBannedFtpUser
return
}
if len(f.NgingFtpUser.Directory) > 0 {
basePath = f.NgingFtpUser.Directory
return
}
if len(basePath) < 1 {
err = ErrNoneFtpDirectory
}
return
}