Пример #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
	})
}
Пример #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)
}
Пример #3
0
func AddApiKey(cmd *m.AddApiKeyCommand) error {
	return inTransaction(func(sess *xorm.Session) error {
		t := m.ApiKey{
			OrgId:   cmd.OrgId,
			Name:    cmd.Name,
			Role:    cmd.Role,
			Key:     cmd.Key,
			Created: time.Now(),
			Updated: time.Now(),
		}

		if _, err := sess.Insert(&t); err != nil {
			return err
		}
		cmd.Result = &t
		return nil
	})
}