// ResetPassword handles request from the reset page and reset password func (cc *CommonController) ResetPassword() { resetUUID := cc.GetString("reset_uuid") if resetUUID == "" { cc.CustomAbort(http.StatusBadRequest, "Reset uuid is blank.") } queryUser := models.User{ResetUUID: resetUUID} user, err := dao.GetUser(queryUser) if err != nil { log.Errorf("Error occurred in GetUser: %v", err) cc.CustomAbort(http.StatusInternalServerError, "Internal error.") } if user == nil { log.Error("User does not exist") cc.CustomAbort(http.StatusBadRequest, "User does not exist") } password := cc.GetString("password") if password != "" { user.Password = password err = dao.ResetUserPassword(*user) if err != nil { log.Errorf("Error occurred in ResetUserPassword: %v", err) cc.CustomAbort(http.StatusInternalServerError, "Internal error.") } } else { cc.CustomAbort(http.StatusBadRequest, "password_is_required") } }
// ChangePassword handles PUT to /api/users/{}/password func (ua *UserAPI) ChangePassword() { ldapAdminUser := (ua.AuthMode == "ldap_auth" && ua.userID == 1 && ua.userID == ua.currentUserID) if !(ua.AuthMode == "db_auth" || ldapAdminUser) { ua.CustomAbort(http.StatusForbidden, "") } if !ua.IsAdmin { if ua.userID != ua.currentUserID { log.Error("Guests can only change their own account.") ua.CustomAbort(http.StatusForbidden, "Guests can only change their own account.") } } var req passwordReq ua.DecodeJSONReq(&req) if req.OldPassword == "" { log.Error("Old password is blank") ua.CustomAbort(http.StatusBadRequest, "Old password is blank") } queryUser := models.User{UserID: ua.userID, Password: req.OldPassword} user, err := dao.CheckUserPassword(queryUser) if err != nil { log.Errorf("Error occurred in CheckUserPassword: %v", err) ua.CustomAbort(http.StatusInternalServerError, "Internal error.") } if user == nil { log.Warning("Password input is not correct") ua.CustomAbort(http.StatusForbidden, "old_password_is_not_correct") } if req.NewPassword == "" { ua.CustomAbort(http.StatusBadRequest, "please_input_new_password") } updateUser := models.User{UserID: ua.userID, Password: req.NewPassword, Salt: user.Salt} err = dao.ChangeUserPassword(updateUser, req.OldPassword) if err != nil { log.Errorf("Error occurred in ChangeUserPassword: %v", err) ua.CustomAbort(http.StatusInternalServerError, "Internal error.") } }
//GetCert gets default self-signed certificate. func (sia *SystemInfoAPI) GetCert() { if sia.isAdmin { if _, err := os.Stat(defaultRootCert); !os.IsNotExist(err) { sia.Ctx.Output.Header("Content-Disposition", "attachment; filename=ca.crt") http.ServeFile(sia.Ctx.ResponseWriter, sia.Ctx.Request, defaultRootCert) } else { log.Error("No certificate found.") sia.CustomAbort(http.StatusNotFound, "No certificate found.") } } sia.CustomAbort(http.StatusUnauthorized, "") }
// Prepare validates the URL and parms func (ia *InternalAPI) Prepare() { var currentUserID int currentUserID = ia.ValidateUser() isAdmin, err := dao.IsAdminRole(currentUserID) if err != nil { log.Errorf("Error occurred in IsAdminRole:%v", err) ia.CustomAbort(http.StatusInternalServerError, "Internal error.") } if !isAdmin { log.Error("Guests doesn't have the permisson to request harbor internal API.") ia.CustomAbort(http.StatusForbidden, "Guests doesn't have the permisson to request harbor internal API.") } }
func main() { beego.BConfig.WebConfig.Session.SessionOn = true //TODO redisURL := os.Getenv("_REDIS_URL") if len(redisURL) > 0 { beego.BConfig.WebConfig.Session.SessionProvider = "redis" beego.BConfig.WebConfig.Session.SessionProviderConfig = redisURL } // beego.AddTemplateExt("htm") dao.InitDatabase() if err := updateInitPassword(adminUserID, config.InitialAdminPassword()); err != nil { log.Error(err) } initRouters() if err := api.SyncRegistry(); err != nil { log.Error(err) } beego.Run() }
// Get checks if reset_uuid in the reset link is valid and render the result page for user to reset password. func (rpc *ResetPasswordController) Get() { resetUUID := rpc.GetString("reset_uuid") if resetUUID == "" { log.Error("Reset uuid is blank.") rpc.Redirect("/", http.StatusFound) return } queryUser := models.User{ResetUUID: resetUUID} user, err := dao.GetUser(queryUser) if err != nil { log.Errorf("Error occurred in GetUser: %v", err) rpc.CustomAbort(http.StatusInternalServerError, "Internal error.") } if user != nil { rpc.Data["ResetUuid"] = user.ResetUUID rpc.Forward("page_title_reset_password", "reset-password.htm") } else { rpc.Redirect("/", http.StatusFound) } }
func clearUp(username string) { var err error o := orm.NewOrm() o.Begin() err = execUpdate(o, `delete from project_member where user_id = ( select user_id from user where username = ? ) `, username) if err != nil { o.Rollback() log.Error(err) } err = execUpdate(o, `delete from project_member where project_id = ( select project_id from project where name = ? )`, projectName) if err != nil { o.Rollback() log.Error(err) } err = execUpdate(o, `delete from access_log where user_id = ( select user_id from user where username = ? )`, username) if err != nil { o.Rollback() log.Error(err) } err = execUpdate(o, `delete from access_log where project_id = ( select project_id from project where name = ? )`, projectName) if err != nil { o.Rollback() log.Error(err) } err = execUpdate(o, `delete from project where name = ?`, projectName) if err != nil { o.Rollback() log.Error(err) } err = execUpdate(o, `delete from user where username = ?`, username) if err != nil { o.Rollback() log.Error(err) } err = execUpdate(o, `delete from replication_job where id < 99`) if err != nil { log.Error(err) } err = execUpdate(o, `delete from replication_policy where id < 99`) if err != nil { log.Error(err) } err = execUpdate(o, `delete from replication_target where id < 99`) if err != nil { log.Error(err) } o.Commit() }
// SyncRegistry syncs the repositories of registry with database. func SyncRegistry() error { log.Debugf("Start syncing repositories from registry to DB... ") reposInRegistry, err := catalog() if err != nil { log.Error(err) return err } var repoRecordsInDB []models.RepoRecord repoRecordsInDB, err = dao.GetAllRepositories() if err != nil { log.Errorf("error occurred while getting all registories. %v", err) return err } var reposInDB []string for _, repoRecordInDB := range repoRecordsInDB { reposInDB = append(reposInDB, repoRecordInDB.Name) } var reposToAdd []string var reposToDel []string reposToAdd, reposToDel, err = diffRepos(reposInRegistry, reposInDB) if err != nil { return err } if len(reposToAdd) > 0 { log.Debugf("Start adding repositories into DB... ") for _, repoToAdd := range reposToAdd { project, _ := utils.ParseRepository(repoToAdd) user, err := dao.GetAccessLogCreator(repoToAdd) if err != nil { log.Errorf("Error happens when getting the repository owner from access log: %v", err) } if len(user) == 0 { user = "******" } pullCount, err := dao.CountPull(repoToAdd) if err != nil { log.Errorf("Error happens when counting pull count from access log: %v", err) } repoRecord := models.RepoRecord{Name: repoToAdd, OwnerName: user, ProjectName: project, PullCount: pullCount} if err := dao.AddRepository(repoRecord); err != nil { log.Errorf("Error happens when adding the missing repository: %v", err) } else { log.Debugf("Add repository: %s success.", repoToAdd) } } } if len(reposToDel) > 0 { log.Debugf("Start deleting repositories from DB... ") for _, repoToDel := range reposToDel { if err := dao.DeleteRepository(repoToDel); err != nil { log.Errorf("Error happens when deleting the repository: %v", err) } else { log.Debugf("Delete repository: %s success.", repoToDel) } } } log.Debugf("Sync repositories from registry to DB is done.") return nil }