Esempio n. 1
0
func AddSystem(cmd *m.AddSystemsCommand) error {
	return inTransaction(func(sess *xorm.Session) error {
		// check if service exists
		var err error
		for _, systemName := range cmd.SystemsName {
			if res, err := sess.Query("SELECT 1 from systems WHERE systems_name=? and org_id=?", systemName, cmd.OrgId); err != nil {
				return err
			} else if len(res) == 1 {
				return m.ErrSystemAlreadyAdded
			}

			entity := m.Systems{
				OrgId:       cmd.OrgId,
				SystemsName: systemName,
				Slug:        slug.Make(systemName),
			}

			_, err = sess.Insert(&entity)

			apiEntity := m.AddApiKeyCommand{}
			apiEntity.OrgId = cmd.OrgId
			apiEntity.Name = strconv.FormatInt(entity.Id, 16)
			apiEntity.Role = m.ROLE_ADMIN
			newKeyInfo := apikeygen.New(apiEntity.OrgId, apiEntity.Name)
			apiEntity.Key = newKeyInfo.ClientSecret
			err = AddApiKey(&apiEntity)
		}
		return err
	})
}
Esempio n. 2
0
func AddApiKey(c *middleware.Context, cmd m.AddApiKeyCommand) Response {
	if !cmd.Role.IsValid() {
		return ApiError(400, "Invalid role specified", nil)
	}

	cmd.OrgId = c.OrgId

	newKeyInfo := apikeygen.New(cmd.OrgId, cmd.Name)
	cmd.Key = newKeyInfo.HashedKey

	if err := bus.Dispatch(&cmd); err != nil {
		return ApiError(500, "Failed to add API key", err)
	}

	result := &dtos.NewApiKeyResult{
		Name: cmd.Result.Name,
		Key:  newKeyInfo.ClientSecret}

	return Json(200, result)
}