Exemplo n.º 1
0
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
}
Exemplo n.º 2
0
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
}
Exemplo n.º 3
0
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
}
Exemplo n.º 4
0
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
}
Exemplo n.º 5
0
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
}
Exemplo n.º 6
0
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
}