Skip to content

Commit 6fcb5fb

Browse files
committed
Separate staff account password changing from rank changing, add staff callback function tests
1 parent bd10390 commit 6fcb5fb

File tree

14 files changed

+724
-441
lines changed

14 files changed

+724
-441
lines changed

frontend/sass/_util.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
color: $a-visited;
3131
}
3232
}
33-
}
33+
}

frontend/sass/global/_img.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ div#upload-box {
122122
.postblock {
123123
margin-left: 8px;
124124
margin-right: 8px;
125-
width: 100px;
125+
// width: 100px;
126126
}
127127

128128
.post-text, .banned-message {

frontend/sass/pipes/_img.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212

1313
.postblock,
14-
table.mgmt-table tr:first-of-type th {
14+
table.mgmt-table th {
1515
background: colors.$postblock;
1616
font-weight: 700;
1717
}

html/css/global.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ div#upload-box .upload-x, div#upload-box .upload-filename {
139139
.postblock {
140140
margin-left: 8px;
141141
margin-right: 8px;
142-
width: 100px;
143142
}
144143

145144
.post-text, .banned-message {

html/css/pipes.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ div#recent-posts-header {
5353
}
5454

5555
.postblock,
56-
table.mgmt-table tr:first-of-type th {
56+
table.mgmt-table th {
5757
background: #25272D;
5858
font-weight: 700;
5959
}

pkg/gcsql/staff.go

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,16 @@ func (s *Staff) UpdateRank(rank int) error {
107107
if rank < 0 || rank > 3 {
108108
return ErrInvalidStaffRank
109109
}
110-
_, err := ExecTimeoutSQL(nil, "UPDATE DBPREFIXstaff SET global_rank = ? WHERE id = ?", rank, s.ID)
111-
if err != nil {
110+
var err error
111+
if s.ID == 0 {
112+
// ID field not set yet, get it from the DB
113+
s.ID, err = GetStaffID(s.Username)
114+
if err != nil {
115+
return err
116+
}
117+
}
118+
119+
if _, err = ExecTimeoutSQL(nil, "UPDATE DBPREFIXstaff SET global_rank = ? WHERE id = ?", rank, s.ID); err != nil {
112120
return err
113121
}
114122
s.Rank = rank
@@ -120,47 +128,28 @@ func (s *Staff) UpdatePassword(password string) error {
120128
if password == "" {
121129
return ErrInvalidStaffPassword
122130
}
131+
var err error
132+
if s.ID == 0 {
133+
// ID field not set yet, get it from the DB
134+
s.ID, err = GetStaffID(s.Username)
135+
if err != nil {
136+
return err
137+
}
138+
}
139+
123140
checksum := gcutil.BcryptSum(password)
124-
_, err := ExecTimeoutSQL(nil, "UPDATE DBPREFIXstaff SET password_checksum = ? WHERE id = ?", checksum, s.ID)
141+
_, err = ExecTimeoutSQL(nil, "UPDATE DBPREFIXstaff SET password_checksum = ? WHERE id = ?", checksum, s.ID)
125142
if err != nil {
126143
return err
127144
}
128145
s.PasswordChecksum = checksum
129146
return nil
130147
}
131148

132-
// UpdateStaff sets the rank and/or password of the staff account with the given username. If password
133-
// is blank, only the rank will be updated
134-
func UpdateStaff(username string, rank int, password string) error {
135-
// first check if it's a recognized username
136-
id, err := GetStaffID(username)
137-
if err != nil {
138-
return err
139-
}
140-
sqlUpdate := "UPDATE DBPREFIXstaff SET global_rank = ?"
141-
args := []any{rank}
142-
if password != "" {
143-
sqlUpdate += ", password_checksum = ?"
144-
args = append(args, gcutil.BcryptSum(password))
145-
}
146-
sqlUpdate += " WHERE id = ?"
147-
args = append(args, id)
148-
149-
_, err = ExecTimeoutSQL(nil, sqlUpdate, args...)
150-
return err
151-
}
152-
153-
// UpdateStaff sets the password of the staff account with the given username
154-
func UpdatePassword(username string, newPassword string) error {
155-
const sqlUPDATE = `UPDATE DBPREFIXstaff SET password_checksum = ? WHERE id = ?`
156-
id, err := GetStaffID(username)
157-
if err != nil {
158-
return err
159-
}
160-
checksum := gcutil.BcryptSum(newPassword)
161-
162-
_, err = ExecTimeoutSQL(nil, sqlUPDATE, checksum, id)
163-
return err
149+
// UpdateStaffPassword sets the password of the staff account with the given username
150+
func UpdateStaffPassword(username string, newPassword string) error {
151+
staff := Staff{Username: username}
152+
return staff.UpdatePassword(newPassword)
164153
}
165154

166155
// EndStaffSession deletes any session rows associated with the requests session cookie and then

pkg/gcsql/tables.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,46 +118,47 @@ func (u *Upload) IsEmbed() bool {
118118
return strings.HasPrefix(u.Filename, "embed:")
119119
}
120120

121-
// IPBanBase used to composition IPBan and IPBanAudit. It does not represent a SQL table by itself
121+
// IPBanBase is used by IPBan and IPBanAudit. It does not represent a SQL table by itself
122122
type IPBanBase struct {
123-
IsActive bool
124-
IsThreadBan bool
125-
ExpiresAt time.Time
126-
StaffID int
127-
AppealAt time.Time
128-
Permanent bool
129-
StaffNote string
130-
Message string
131-
CanAppeal bool
123+
IsActive bool // sql: is_active
124+
IsThreadBan bool // sql: is_thread_ban
125+
ExpiresAt time.Time // sql: expires_at
126+
StaffID int // sql: staff_id
127+
AppealAt time.Time // sql: appeal_at
128+
Permanent bool // sql: permanent
129+
StaffNote string // sql: staff_note
130+
Message string // sql: message
131+
CanAppeal bool // sql: can_appeal
132132
}
133133

134134
// IPBan contains the information association with a specific ip ban.
135135
// table: DBPREFIXip_ban
136136
type IPBan struct {
137-
ID int
138-
BoardID *int
139-
BannedForPostID *int
140-
CopyPostText template.HTML
141-
RangeStart string
142-
RangeEnd string
143-
IssuedAt time.Time
137+
ID int // sql: id
138+
BoardID *int // sql: board_id
139+
BannedForPostID *int // sql: banned_for_post_id
140+
CopyPostText template.HTML // sql: copy_post_text
141+
RangeStart string // sql: range_start
142+
RangeEnd string // sql: range_end
143+
IssuedAt time.Time // sql: issued_at
144144
IPBanBase
145145
}
146146

147147
// Deprecated: Use the RangeStart and RangeEnd fields or gcutil.GetIPRangeSubnet.
148148
// IP was previously a field in the IPBan struct before range bans were
149149
// implemented. This is here as a fallback for templates
150-
func (ipb *IPBan) IP() string {
150+
func (ipb *IPBan) IP() (string, error) {
151151
if ipb.RangeStart == ipb.RangeEnd {
152-
return ipb.RangeStart
152+
return ipb.RangeStart, nil
153153
}
154154
inet, err := gcutil.GetIPRangeSubnet(ipb.RangeStart, ipb.RangeEnd)
155155
if err != nil {
156-
return "?"
156+
return "", err
157157
}
158-
return inet.String()
158+
return inet.String(), nil
159159
}
160160

161+
// IsBanned returns true if the given IP is banned
161162
func (ipb *IPBan) IsBanned(ipStr string) (bool, error) {
162163
ipn, err := gcutil.GetIPRangeSubnet(ipb.RangeStart, ipb.RangeEnd)
163164
if err != nil {

pkg/manage/actionsAdminPerm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import (
1616
"github.com/gochan-org/gochan/pkg/gctemplates"
1717
"github.com/gochan-org/gochan/pkg/gcutil"
1818
"github.com/gochan-org/gochan/pkg/posting"
19+
"github.com/gochan-org/gochan/pkg/server"
1920
"github.com/gochan-org/gochan/pkg/server/serverutil"
2021
"github.com/rs/zerolog"
2122
)
2223

2324
var (
24-
ErrInsufficientPermission = errors.New("insufficient account permission")
25+
ErrInsufficientPermission = server.NewServerError("insufficient account permission", http.StatusForbidden)
2526
)
2627

2728
type uploadInfo struct {

0 commit comments

Comments
 (0)