func (this *UserWebAPIV1Controller) GetUsers() { user := new(models.User) users := user.All() this.JSONOut(http.StatusOK, "", users) return }
func (this *OrganizationWebV1Controller) GetJoinOrgs() { user := new(models.User) orgs := make([]models.Organization, 0) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } for _, name := range user.JoinOrganizations { org := new(models.Organization) if err := org.GetByName(name); err != nil { this.JSONOut(http.StatusBadRequest, "Get organizations error", nil) return } orgs = append(orgs, *org) } this.JSONOut(http.StatusOK, "", orgs) return }
func (this *RepoAPIV1Controller) PutRepository() { username, _, _ := utils.DecodeBasicAuth(this.Ctx.Input.Header("Authorization")) namespace := string(this.Ctx.Input.Param(":namespace")) repository := string(this.Ctx.Input.Param(":repo_name")) repo := new(models.Repository) if err := repo.Put(namespace, repository, string(this.Ctx.Input.CopyBody()), this.Ctx.Input.Header("User-Agent"), models.APIVERSION_V1); err != nil { this.JSONOut(http.StatusForbidden, err.Error(), nil) return } if this.Ctx.Input.Header("X-Docker-Token") == "true" { token := string(utils.GeneralKey(username)) this.SetSession("token", token) this.Ctx.Output.Context.ResponseWriter.Header().Set("X-Docker-Token", token) this.Ctx.Output.Context.ResponseWriter.Header().Set("WWW-Authenticate", token) } user := new(models.User) if _, _, err := user.Has(username); err != nil { this.JSONOut(http.StatusForbidden, err.Error(), nil) return } memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_UPDATE_REPO, models.LEVELINFORMATIONAL, models.TYPE_APIV1, repo.Id, memo) repo.Log(models.ACTION_UPDATE_REPO, models.LEVELINFORMATIONAL, models.TYPE_APIV1, repo.Id, memo) this.Ctx.Output.Context.ResponseWriter.Header().Set("X-Docker-Endpoints", beego.AppConfig.String("docker::Endpoints")) this.Ctx.Output.Context.Output.SetStatus(http.StatusOK) this.Ctx.Output.Context.Output.Body([]byte("")) return }
func (this *OrganizationWebV1Controller) GetTeams() { user := new(models.User) teams := make([]models.Team, 0) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } for _, name := range user.Teams { team := new(models.Team) if exist, _, err := team.Has(strings.Split(name, "-")[0], strings.Split(name, "-")[1]); err != nil { this.JSONOut(http.StatusBadRequest, "Get Team error", nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Team invalid", nil) return } teams = append(teams, *team) } this.JSONOut(http.StatusOK, "", teams) return }
func (this *OrganizationWebV1Controller) GetTeam() { user := new(models.User) org := new(models.Organization) team := new(models.Team) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } if exist, _, err := org.Has(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Organization not exist", nil) return } if exist, _, err := team.Has(this.Ctx.Input.Param(":org"), this.Ctx.Input.Param(":team")); err != nil { this.JSONOut(http.StatusBadRequest, "Search team error", nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Team not exist", nil) return } this.JSONOut(http.StatusOK, "", team) return }
func (this *UserWebAPIV1Controller) PutPassword() { user := new(models.User) var p map[string]interface{} if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &p); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false && err == nil { this.JSONOut(http.StatusBadRequest, "Search user error", nil) return } else if p["oldPassword"].(string) != user.Password { this.JSONOut(http.StatusBadRequest, "account and password not match", nil) return } user.Password = p["newPassword"].(string) user.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := user.Save(); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_UPDATE_PASSWORD, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo) this.JSONOut(http.StatusOK, "Update password success!", nil) return }
func (this *TeamWebV1Controller) PutTeamAddMember() { if _, exist := this.Ctx.Input.CruSession.Get("user").(models.User); exist == false { this.JSONOut(http.StatusBadRequest, "", map[string]string{"message": "Session load failure", "url": "/auth"}) return } org := new(models.Organization) if err := org.GetByName(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } team := new(models.Team) if err := team.GetByName(this.Ctx.Input.Param(":org"), this.Ctx.Input.Param(":team")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } user := new(models.User) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not found", nil) return } exist := false for _, u := range team.Users { if u == this.Ctx.Input.Param(":username") { exist = true } } if exist == true { this.JSONOut(http.StatusBadRequest, "User already in team", nil) return } else { team.Users = append(team.Users, this.Ctx.Input.Param(":username")) if err := team.Save(); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } this.JSONOut(http.StatusOK, "", user) return } }
func (this *UserWebAPIV1Controller) GetUser() { user := new(models.User) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false && err == nil { this.JSONOut(http.StatusBadRequest, "Search user error", nil) return } user.Password = "******" this.JSONOut(http.StatusOK, "", user) return }
func (this *OrganizationWebV1Controller) PutTeam() { user := new(models.User) org := new(models.Organization) team := new(models.Team) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } if exist, _, err := org.Has(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Organization not exist", nil) return } if exist, _, err := team.Has(this.Ctx.Input.Param(":org"), this.Ctx.Input.Param(":team")); err != nil { this.JSONOut(http.StatusBadRequest, "Search team error", nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Team not exist", nil) return } if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &team); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } team.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := team.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "Team save error", nil) return } this.JSONOut(http.StatusOK, "Team update successfully", nil) return }
func (this *UserWebAPIV1Controller) GetNamespaces() { user := new(models.User) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false && err == nil { this.JSONOut(http.StatusBadRequest, "Search user error", nil) return } else { namespaces := make([]string, 0) namespaces = append(namespaces, user.Username) namespaces = append(namespaces, user.Organizations...) this.JSONOut(http.StatusOK, "", namespaces) return } }
//There is nothing in request body, just authorization through Basic Authorization. func (this *UserAPIV1Controller) GetUsers() { if username, passwd, err := utils.DecodeBasicAuth(this.Ctx.Input.Header("Authorization")); err != nil { this.JSONOut(http.StatusUnauthorized, err.Error(), nil) return } else { user := new(models.User) if err := user.Get(username, passwd); err != nil { this.JSONOut(http.StatusUnauthorized, err.Error(), nil) return } memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_SIGNUP, models.LEVELINFORMATIONAL, models.TYPE_APIV1, user.Id, memo) this.JSONOut(http.StatusOK, "User authorization successfully.", nil) return } }
func (this *UserWebAPIV1Controller) Signin() { user := new(models.User) if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &user); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else { if err := user.Get(user.Username, user.Password); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_SIGNIN, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo) this.Ctx.Input.CruSession.Set("user", user) this.JSONOut(http.StatusOK, "User singin successfully!", nil) return } }
func (this *UserWebAPIV1Controller) Signup() { user := new(models.User) org := new(models.Organization) if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &user); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else { if exist, _, err := org.Has(user.Username); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == true { this.JSONOut(http.StatusBadRequest, "Namespace is occupation already by organization.", nil) return } if exist, _, err := user.Has(user.Username); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == true { this.JSONOut(http.StatusBadRequest, "User already exist.", nil) return } else { user.Id = string(utils.GeneralKey(user.Username)) user.Created = time.Now().UnixNano() / int64(time.Millisecond) user.Gravatar = "/static/images/default-user-icon-profile.png" if err := user.Save(); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_SIGNUP, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo) this.JSONOut(http.StatusOK, "User singup successfully!", nil) return } } }
func (this *OrganizationWebV1Controller) PutOrg() { user := new(models.User) org := new(models.Organization) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } if exist, _, err := org.Has(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Organization not exist", nil) return } if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &org); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } org.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := org.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "Organization save error", nil) return } memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_UPDATE_ORG, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, org.Id, memo) org.Log(models.ACTION_UPDATE_ORG, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo) this.JSONOut(http.StatusOK, "Update organization successfully", nil) return }
func (this *PingAPIV2Controller) GetPing() { if len(this.Ctx.Input.Header("Authorization")) == 0 { this.JSONOut(http.StatusUnauthorized, "", map[string][]modules.ErrorDescriptor{"errors": []modules.ErrorDescriptor{modules.ErrorDescriptors[modules.APIErrorCodeUnauthorized]}}) return } if username, passwd, err := utils.DecodeBasicAuth(this.Ctx.Input.Header("Authorization")); err != nil { this.JSONOut(http.StatusUnauthorized, "", map[string][]modules.ErrorDescriptor{"errors": []modules.ErrorDescriptor{modules.ErrorDescriptors[modules.APIErrorCodeUnauthorized]}}) return } else { user := new(models.User) if err := user.Get(username, passwd); err != nil { this.JSONOut(http.StatusUnauthorized, "", map[string][]modules.ErrorDescriptor{"errors": []modules.ErrorDescriptor{modules.ErrorDescriptors[modules.APIErrorCodeUnauthorized]}}) return } memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_SIGNUP, models.LEVELINFORMATIONAL, models.TYPE_APIV2, user.Id, memo) this.JSONOut(http.StatusOK, "", "User authorization successfully.") return } }
func (this *UserWebAPIV1Controller) PutProfile() { user := new(models.User) var p map[string]interface{} if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false && err == nil { this.JSONOut(http.StatusBadRequest, "Search user error", nil) return } if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &p); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } if strings.Contains(fmt.Sprint(p["gravatar"]), "resize") { suffix := strings.Split(fmt.Sprint(p["gravatar"]), ".")[1] gravatar := fmt.Sprintf("%s%s%s%s%s", beego.AppConfig.String("gravatar"), "/", user.Username, "_gravatar.", suffix) if _, err := os.Stat(gravatar); err == nil { os.Remove(gravatar) } os.Rename(fmt.Sprint(p["gravatar"]), gravatar) p["gravatar"] = gravatar } user.Email, user.Fullname, user.Mobile = p["email"].(string), p["fullname"].(string), p["mobile"].(string) user.Gravatar, user.Company, user.URL = p["gravatar"].(string), p["company"].(string), p["url"].(string) user.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := user.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "User save failure", nil) return } this.Ctx.Input.CruSession.Set("user", user) memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_UPDATE_PROFILE, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo) this.JSONOut(http.StatusOK, "Update Profile Successfully!", nil) return }
func (this *RepoAPIV1Controller) PutRepositoryImages() { namespace := this.Ctx.Input.Param(":namespace") repository := this.Ctx.Input.Param(":repo_name") repo := new(models.Repository) if err := repo.PutImages(namespace, repository); err != nil { this.JSONOut(http.StatusBadRequest, "Update Uploaded flag error", nil) return } memo, _ := json.Marshal(this.Ctx.Input.Header) repo.Log(models.ACTION_PUT_REPO_IMAGES, models.LEVELINFORMATIONAL, models.TYPE_APIV1, repo.Id, memo) org := new(models.Organization) isOrg, _, err := org.Has(namespace) if err != nil { this.JSONOut(http.StatusBadRequest, "Search Organization Error", nil) return } user := new(models.User) authUsername, _, _ := utils.DecodeBasicAuth(this.Ctx.Input.Header("Authorization")) isUser, _, err := user.Has(authUsername) if err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } if !isUser && !isOrg { this.JSONOut(http.StatusBadRequest, "Search Namespace Error", nil) return } if isUser { user.Repositories = append(user.Repositories, repo.Id) user.Save() } if isOrg { org.Repositories = append(org.Repositories, repo.Id) org.Save() } this.Ctx.Output.Context.Output.SetStatus(http.StatusNoContent) this.Ctx.Output.Context.Output.Body([]byte("")) this.ServeJson() return }
func (this *OrganizationWebV1Controller) PostOrg() { user := new(models.User) org := new(models.Organization) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } if exist, _, err := user.Has(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == true { this.JSONOut(http.StatusBadRequest, "Namespace is occupation already by another user", nil) return } if exist, _, err := org.Has(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == true { this.JSONOut(http.StatusBadRequest, "Namespace is occupation already by another organization", nil) return } if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &org); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } org.Id = string(utils.GeneralKey(org.Name)) org.Username = user.Username org.Created = time.Now().UnixNano() / int64(time.Millisecond) org.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := org.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "Organization save error", nil) return } user.Organizations = append(user.Organizations, org.Name) user.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := user.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "User save error", nil) return } memo, _ := json.Marshal(this.Ctx.Input.Header) user.Log(models.ACTION_ADD_ORG, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, org.Id, memo) org.Log(models.ACTION_ADD_ORG, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo) this.JSONOut(http.StatusOK, "Create organization successfully.", nil) return }
func (this *OrganizationWebV1Controller) PostTeam() { user := new(models.User) org := new(models.Organization) team := new(models.Team) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } if exist, _, err := org.Has(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Organization not exist", nil) return } if exist, _, err := team.Has(this.Ctx.Input.Param(":org"), this.Ctx.Input.Param(":team")); err != nil { this.JSONOut(http.StatusBadRequest, "Search team error", nil) return } else if exist == true { this.JSONOut(http.StatusBadRequest, "Team already exist", nil) return } if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &team); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } team.Id = fmt.Sprintf("%s-%s", this.Ctx.Input.Param(":org"), this.Ctx.Input.Param(":team")) team.Username = this.Ctx.Input.Param(":username") team.Users, team.Repositories = []string{this.Ctx.Input.Param(":username")}, []string{} team.Created = time.Now().UnixNano() / int64(time.Millisecond) team.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := team.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "Team save error", nil) return } org.Teams = append(org.Teams, team.Id) org.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := org.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "Organization save error", nil) return } user.Teams = append(user.Teams, team.Id) user.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := user.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "User save error", nil) return } this.JSONOut(http.StatusOK, "Team create successfully", nil) return }
func (this *UserWebAPIV1Controller) PostGravatar() { user := new(models.User) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false && err == nil { this.JSONOut(http.StatusBadRequest, "Search user error", nil) return } file, fileHeader, err := this.Ctx.Request.FormFile("file") if err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } prefix := strings.Split(fileHeader.Filename, ".")[0] suffix := strings.Split(fileHeader.Filename, ".")[1] if suffix != "png" && suffix != "jpg" && suffix != "jpeg" { this.JSONOut(http.StatusBadRequest, "gravatar must be .jpg,.jepg or .png", nil) return } if _, err := os.Stat(fmt.Sprintf("%s%s%s%s%s", beego.AppConfig.String("gravatar"), "/", prefix, "_resize.", suffix)); err == nil { os.Remove(fmt.Sprintf("%s%s%s%s%s", beego.AppConfig.String("gravatar"), "/", prefix, "_resize.", suffix)) } f, err := os.OpenFile(fmt.Sprintf("%s%s%s", beego.AppConfig.String("gravatar"), "/", fileHeader.Filename), os.O_WRONLY|os.O_CREATE, 0666) if err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } io.Copy(f, file) f.Close() // decode jpeg into image.Image var img image.Image imageFile, err := os.Open(fmt.Sprintf("%s%s%s", beego.AppConfig.String("gravatar"), "/", fileHeader.Filename)) if err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } switch suffix { case "png": img, err = png.Decode(imageFile) case "jpg": img, err = jpeg.Decode(imageFile) case "jpeg": img, err = jpeg.Decode(imageFile) } if err != nil { imageFile.Close() this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } imageFile.Close() m := resize.Resize(100, 100, img, resize.Lanczos3) out, err := os.Create(fmt.Sprintf("%s%s%s%s%s", beego.AppConfig.String("gravatar"), "/", prefix, "_resize.", suffix)) if err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } defer out.Close() // write new image to file switch suffix { case "png": png.Encode(out, m) case "jpg": jpeg.Encode(out, m, nil) case "jpeg": jpeg.Encode(out, m, nil) } os.Remove(fmt.Sprintf("%s%s%s", beego.AppConfig.String("gravatar"), "/", fileHeader.Filename)) url := fmt.Sprintf("%s%s%s%s%s", beego.AppConfig.String("gravatar"), "/", prefix, "_resize.", suffix) user.Gravatar = url if err := user.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "User save failure", nil) return } this.Ctx.Input.CruSession.Set("user", user) this.JSONOut(http.StatusOK, "", map[string]string{"message": "Please click button to finish uploading gravatar", "url": url}) return }
func (this *OrganizationWebV1Controller) PostMember() { user := new(models.User) org := new(models.Organization) team := new(models.Team) member := new(models.User) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } if exist, _, err := org.Has(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Organization not exist", nil) return } if exist, _, err := team.Has(this.Ctx.Input.Param(":org"), this.Ctx.Input.Param(":team")); err != nil { this.JSONOut(http.StatusBadRequest, "Search team error", nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Team not exist", nil) return } if exist, _, err := user.Has(this.Ctx.Input.Param(":member")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } team.Users = append(team.Users, member.Username) team.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := team.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "Team save error", nil) return } has := false for _, k := range member.JoinOrganizations { if k == org.Name { has = true } } if has == false { member.JoinOrganizations = append(member.JoinOrganizations, org.Name) } member.JoinTeams = append(member.JoinTeams, team.Id) member.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := member.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "User save error", nil) return } this.JSONOut(http.StatusOK, "User added to the team", nil) return }
func (this *OrganizationWebV1Controller) PutMember() { user := new(models.User) org := new(models.Organization) team := new(models.Team) member := new(models.User) if exist, _, err := user.Has(this.Ctx.Input.Param(":username")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } if exist, _, err := org.Has(this.Ctx.Input.Param(":org")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Organization not exist", nil) return } if exist, _, err := team.Has(this.Ctx.Input.Param(":org"), this.Ctx.Input.Param(":team")); err != nil { this.JSONOut(http.StatusBadRequest, "Search team error", nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Team not exist", nil) return } if exist, _, err := user.Has(this.Ctx.Input.Param(":member")); err != nil { this.JSONOut(http.StatusBadRequest, err.Error(), nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "User not exist", nil) return } for i, v := range team.Users { if v == member.Username { team.Users = append(team.Users[:i], team.Users[i+1:]...) team.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := team.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "Team save error", nil) return } } } for i, v := range user.JoinTeams { if v == team.Id { user.JoinTeams = append(user.JoinTeams[:i], user.JoinTeams[i+1:]...) user.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := user.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "User save error", nil) return } } } for _, v := range org.Teams { t := new(models.Team) if exist, _, err := team.Has(strings.Split(v, "-")[0], strings.Split(v, "-")[1]); err != nil { this.JSONOut(http.StatusBadRequest, "Search team error", nil) return } else if exist == false { this.JSONOut(http.StatusBadRequest, "Team not exist", nil) return } for _, u := range t.Users { if u == member.Username { this.JSONOut(http.StatusOK, "User remove successfully.", nil) return } } } for i, v := range user.JoinOrganizations { if v == org.Name { user.JoinOrganizations = append(user.JoinOrganizations[:i], user.JoinOrganizations[i+1:]...) user.Updated = time.Now().UnixNano() / int64(time.Millisecond) if err := user.Save(); err != nil { this.JSONOut(http.StatusBadRequest, "User save error", nil) return } } } this.JSONOut(http.StatusOK, "User remove successfully.", nil) return }
func FilterAuth(ctx *context.Context) { var namespace, repository string var permission int auth := true user := new(models.User) namespace = strings.Split(string(ctx.Input.Params[":splat"]), "/")[0] repository = strings.Split(string(ctx.Input.Params[":splat"]), "/")[1] //Get Permission permission = getPermission(ctx.Input.Method()) //Check Authorization In Header if len(ctx.Input.Header("Authorization")) == 0 || strings.Index(ctx.Input.Header("Authorization"), "Basic") == -1 { auth = false goto AUTH } //Check Username, Password And Get User if username, passwd, err := utils.DecodeBasicAuth(ctx.Input.Header("Authorization")); err != nil { auth = false goto AUTH } else { if err := user.Get(username, passwd); err != nil { auth = false goto AUTH } } //Docker Registry V1 Image Don't Check User/Org Permission if isImageResource(ctx.Request.URL.String()) == true { goto AUTH } //Username != namespace if user.Username != namespace { u := new(models.User) if has, _, err := u.Has(namespace); err != nil { auth = false goto AUTH } else if has == false { //Org Repository Check auth = checkOrgRepositoryPermission(user, namespace, repository, permission) } else if has == true { //Different User and Public/Private Repository auth = checkRepositoriesPrivate(namespace, repository) } } AUTH: if auth == false { result := map[string][]modules.ErrorDescriptor{"errors": []modules.ErrorDescriptor{modules.ErrorDescriptors[modules.APIErrorCodeUnauthorized]}} data, _ := json.Marshal(result) ctx.Output.Context.Output.SetStatus(http.StatusNotFound) ctx.Output.Context.Output.Body(data) return } }