fix: 1pctl 增加非 root 提示信息 (#2112)

Refs #2100
This commit is contained in:
ssongliu 2023-08-30 10:28:14 +08:00 committed by wanghe-fit2cloud
parent cbf4f8ec36
commit 9800e7e03d
5 changed files with 45 additions and 1 deletions

View File

@ -3,11 +3,12 @@ package cmd
import (
"bytes"
"fmt"
"io"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/1Panel-dev/1Panel/cmd/server/app"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"io"
)
var (
@ -31,6 +32,10 @@ var initCmd = &cobra.Command{
Use: "init",
Short: "初始化应用",
RunE: func(cmd *cobra.Command, args []string) error {
if !isRoot() {
fmt.Println("请使用 sudo 1pctl app init 或者切换到 root 用户")
return nil
}
if len(args) > 0 {
appKey = args[0]
if len(args) > 1 {

View File

@ -1,6 +1,8 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
@ -22,6 +24,10 @@ var resetMFACmd = &cobra.Command{
Use: "mfa",
Short: "取消 1Panel 两步验证",
RunE: func(cmd *cobra.Command, args []string) error {
if !isRoot() {
fmt.Println("请使用 sudo 1pctl reset mfa 或者切换到 root 用户")
return nil
}
db, err := loadDBConn()
if err != nil {
return err
@ -34,6 +40,10 @@ var resetSSLCmd = &cobra.Command{
Use: "https",
Short: "取消 1Panel https 方式登录",
RunE: func(cmd *cobra.Command, args []string) error {
if !isRoot() {
fmt.Println("请使用 sudo 1pctl reset https 或者切换到 root 用户")
return nil
}
db, err := loadDBConn()
if err != nil {
return err
@ -46,6 +56,10 @@ var resetEntranceCmd = &cobra.Command{
Use: "entrance",
Short: "取消 1Panel 安全入口",
RunE: func(cmd *cobra.Command, args []string) error {
if !isRoot() {
fmt.Println("请使用 sudo 1pctl reset entrance 或者切换到 root 用户")
return nil
}
db, err := loadDBConn()
if err != nil {
return err
@ -58,6 +72,10 @@ var resetBindIpsCmd = &cobra.Command{
Use: "ips",
Short: "取消 1Panel 授权 IP 限制",
RunE: func(cmd *cobra.Command, args []string) error {
if !isRoot() {
fmt.Println("请使用 sudo 1pctl reset ips 或者切换到 root 用户")
return nil
}
db, err := loadDBConn()
if err != nil {
return err
@ -70,6 +88,10 @@ var resetDomainCmd = &cobra.Command{
Use: "domain",
Short: "取消 1Panel 访问域名绑定",
RunE: func(cmd *cobra.Command, args []string) error {
if !isRoot() {
fmt.Println("请使用 sudo 1pctl reset domain 或者切换到 root 用户")
return nil
}
db, err := loadDBConn()
if err != nil {
return err

View File

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"os/user"
"strings"
"time"
@ -61,3 +62,11 @@ func getSettingByKey(db *gorm.DB, key string) string {
func setSettingByKey(db *gorm.DB, key, value string) error {
return db.Model(&setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error
}
func isRoot() bool {
currentUser, err := user.Current()
if err != nil {
return false
}
return currentUser.Uid == "0"
}

View File

@ -16,6 +16,10 @@ var userinfoCmd = &cobra.Command{
Use: "user-info",
Short: "获取用户信息",
RunE: func(cmd *cobra.Command, args []string) error {
if !isRoot() {
fmt.Println("请使用 sudo 1pctl user-info 或者切换到 root 用户")
return nil
}
db, err := loadDBConn()
if err != nil {
return fmt.Errorf("init my db conn failed, err: %v \n", err)

View File

@ -18,6 +18,10 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "获取系统版本信息",
RunE: func(cmd *cobra.Command, args []string) error {
if !isRoot() {
fmt.Println("请使用 sudo 1pctl version 或者切换到 root 用户")
return nil
}
db, err := loadDBConn()
if err != nil {
return err