Skip to content

Commit

Permalink
fix: don't allow to remove root user
Browse files Browse the repository at this point in the history
  • Loading branch information
o1egl committed Jan 11, 2021
1 parent 8cea2f7 commit 019ce80
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ var (
ErrPermissionDenied = errors.New("permission denied")
ErrInvalidRequestParams = errors.New("invalid request params")
ErrSourceIsParent = errors.New("source is parent")
ErrRootUserDeletion = errors.New("user with id 1 can't be deleted")
)
4 changes: 2 additions & 2 deletions frontend/src/utils/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ Vue.prototype.$showSuccess = (message) => {
})).show()
}

Vue.prototype.$showError = (error) => {
Vue.prototype.$showError = (error, displayReport = true) => {
let btns = [
Noty.button(i18n.t('buttons.close'), '', function () {
n.close()
})
]

if (!disableExternal) {
if (!disableExternal && displayReport) {
btns.unshift(Noty.button(i18n.t('buttons.reportIssue'), '', function () {
window.open('https://github.com/filebrowser/filebrowser/issues/new/choose')
}))
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/settings/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default {
this.$router.push({ path: '/settings/users' })
this.$showSuccess(this.$t('settings.userDeleted'))
} catch (e) {
this.$showError(e)
(e.message === "403") ? this.$showError(this.$t("errors.forbidden"), false) : this.$showError(e)
}
},
async save (event) {
Expand Down
4 changes: 2 additions & 2 deletions http/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ var userGetHandler = withSelfOrAdmin(func(w http.ResponseWriter, r *http.Request

var userDeleteHandler = withSelfOrAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
err := d.store.Users.Delete(d.raw.(uint))
if err == errors.ErrNotExist {
return http.StatusNotFound, err
if err != nil {
return errToStatus(err), err
}

return http.StatusOK, nil
Expand Down
2 changes: 2 additions & 0 deletions http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func errToStatus(err error) int {
return http.StatusForbidden
case errors.Is(err, libErrors.ErrInvalidRequestParams):
return http.StatusBadRequest
case errors.Is(err, libErrors.ErrRootUserDeletion):
return http.StatusForbidden
default:
return http.StatusInternalServerError
}
Expand Down
20 changes: 14 additions & 6 deletions users/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,25 @@ func (s *Storage) Save(user *User) error {
// Delete allows you to delete a user by its name or username. The provided
// id must be a string for username lookup or a uint for id lookup. If id
// is neither, a ErrInvalidDataType will be returned.
func (s *Storage) Delete(id interface{}) (err error) {
func (s *Storage) Delete(id interface{}) error {
switch id := id.(type) {
case string:
err = s.back.DeleteByUsername(id)
user, err := s.back.GetBy(id)
if err != nil {
return err
}
if user.ID == 1 {
return errors.ErrRootUserDeletion
}
return s.back.DeleteByUsername(id)
case uint:
err = s.back.DeleteByID(id)
if id == 1 {
return errors.ErrRootUserDeletion
}
return s.back.DeleteByID(id)
default:
err = errors.ErrInvalidDataType
return errors.ErrInvalidDataType
}

return
}

// LastUpdate gets the timestamp for the last update of an user.
Expand Down

0 comments on commit 019ce80

Please sign in to comment.