MCPcopy Index your code
hub / github.com/1Panel-dev/1Panel / UpdateAuthBasic

Method UpdateAuthBasic

agent/app/service/website_auth_basic.go:63–185  ·  view source on GitHub ↗
(req request.NginxAuthUpdate)

Source from the content-addressed store, hash-verified

61}
62
63func (w WebsiteService) UpdateAuthBasic(req request.NginxAuthUpdate) (err error) {
64 var (
65 website model.Website
66 params []dto.NginxParam
67 authContent []byte
68 authArray []string
69 )
70 website, err = websiteRepo.GetFirst(repo.WithByID(req.WebsiteID))
71 if err != nil {
72 return err
73 }
74 authPath := fmt.Sprintf("/www/sites/%s/auth_basic/auth.pass", website.Alias)
75 absoluteAuthPath := GetSitePath(website, SiteRootAuthBasicPath)
76 fileOp := files.NewFileOp()
77 if !fileOp.Stat(path.Dir(absoluteAuthPath)) {
78 _ = fileOp.CreateDir(path.Dir(absoluteAuthPath), constant.DirPerm)
79 }
80 if !fileOp.Stat(absoluteAuthPath) {
81 _ = fileOp.CreateFile(absoluteAuthPath)
82 }
83
84 params = append(params, dto.NginxParam{Name: "auth_basic", Params: []string{`"Authentication"`}})
85 params = append(params, dto.NginxParam{Name: "auth_basic_user_file", Params: []string{authPath}})
86 authContent, err = fileOp.GetContent(absoluteAuthPath)
87 if err != nil {
88 return
89 }
90 if len(authContent) > 0 {
91 authArray = strings.Split(string(authContent), "\n")
92 }
93 switch req.Operate {
94 case "disable":
95 return deleteNginxConfig(constant.NginxScopeServer, params, &website)
96 case "enable":
97 return updateNginxConfig(constant.NginxScopeServer, params, &website)
98 case "create":
99 for _, line := range authArray {
100 authParams := strings.Split(line, ":")
101 username := authParams[0]
102 if username == req.Username {
103 err = buserr.New("ErrUsernameIsExist")
104 return
105 }
106 }
107 var passwdHash []byte
108 passwdHash, err = bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
109 if err != nil {
110 return
111 }
112 line := fmt.Sprintf("%s:%s\n", req.Username, passwdHash)
113 if req.Remark != "" {
114 line = fmt.Sprintf("%s:%s:%s\n", req.Username, passwdHash, req.Remark)
115 }
116 authArray = append(authArray, line)
117 case "edit":
118 userExist := false
119 for index, line := range authArray {
120 authParams := strings.Split(line, ":")

Callers

nothing calls this directly

Calls 12

StatMethod · 0.95
CreateDirMethod · 0.95
CreateFileMethod · 0.95
GetContentMethod · 0.95
GetSitePathFunction · 0.85
deleteNginxConfigFunction · 0.85
updateNginxConfigFunction · 0.70
GetFirstMethod · 0.65
WithByIDMethod · 0.65
CreateMethod · 0.65
CloseMethod · 0.65
FlushMethod · 0.45

Tested by

no test coverage detected