zitadel/internal/query/action.go
Livio Amstutz ed80a8bb1e
feat: actions (#2377)
* feat(actions): begin api

* feat(actions): begin api

* api and projections

* fix: handle multiple statements for a single event in projections

* export func type

* fix test

* update to new reduce interface

* flows in login

* feat: jwt idp

* feat: command side

* feat: add tests

* actions and flows

* fill idp views with jwt idps and return apis

* add jwtEndpoint to jwt idp

* begin jwt request handling

* add feature

* merge

* merge

* handle jwt idp

* cleanup

* bug fixes

* autoregister

* get token from specific header name

* fix: proto

* fixes

* i18n

* begin tests

* fix and log http proxy

* remove docker cache

* fixes

* usergrants in actions api

* tests adn cleanup

* cleanup

* fix add user grant

* set login context

* i18n

Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2021-09-27 13:43:49 +02:00

105 lines
2.9 KiB
Go

package query
import (
"context"
"time"
"github.com/Masterminds/squirrel"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
)
var actionsQuery = squirrel.StatementBuilder.Select("creation_date", "change_date", "resource_owner", "sequence", "id", "action_state", "name", "script", "timeout", "allowed_to_fail").
From("zitadel.projections.actions").PlaceholderFormat(squirrel.Dollar)
func (q *Queries) GetAction(ctx context.Context, id string, orgID string) (*Action, error) {
idQuery, _ := newActionIDSearchQuery(id)
actions, err := q.SearchActions(ctx, &ActionSearchQueries{Queries: []SearchQuery{idQuery}})
if err != nil {
return nil, err
}
if len(actions) != 1 {
return nil, errors.ThrowNotFound(nil, "QUERY-dft2g", "Errors.Action.NotFound")
}
return actions[0], err
}
func (q *Queries) SearchActions(ctx context.Context, query *ActionSearchQueries) ([]*Action, error) {
stmt, args, err := query.ToQuery(actionsQuery).ToSql()
if err != nil {
return nil, errors.ThrowInvalidArgument(err, "QUERY-wQ3by", "Errors.orgs.invalid.request")
}
rows, err := q.client.QueryContext(ctx, stmt, args...)
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-M6mYN", "Errors.orgs.internal")
}
actions := []*Action{}
for rows.Next() {
org := new(Action)
rows.Scan(
&org.CreationDate,
&org.ChangeDate,
&org.ResourceOwner,
&org.Sequence,
&org.ID,
&org.State,
&org.Name,
&org.Script,
&org.Timeout,
&org.AllowedToFail,
)
actions = append(actions, org)
}
if err := rows.Err(); err != nil {
return nil, errors.ThrowInternal(err, "QUERY-pA0Wj", "Errors.actions.internal")
}
return actions, nil
}
type Action struct {
ID string `col:"id"`
CreationDate time.Time `col:"creation_date"`
ChangeDate time.Time `col:"change_date"`
ResourceOwner string `col:"resource_owner"`
State domain.ActionState `col:"action_state"`
Sequence uint64 `col:"sequence"`
Name string `col:"name"`
Script string `col:"script"`
Timeout time.Duration `col:"-"`
AllowedToFail bool `col:"-"`
}
type ActionSearchQueries struct {
SearchRequest
Queries []SearchQuery
}
func (q *ActionSearchQueries) ToQuery(query squirrel.SelectBuilder) squirrel.SelectBuilder {
query = q.SearchRequest.ToQuery(query)
for _, q := range q.Queries {
query = q.ToQuery(query)
}
return query
}
func NewActionResourceOwnerQuery(id string) (SearchQuery, error) {
return NewTextQuery("resource_owner", id, TextEquals)
}
func NewActionNameSearchQuery(method TextComparison, value string) (SearchQuery, error) {
return NewTextQuery("name", value, method)
}
func NewActionStateSearchQuery(value domain.ActionState) (SearchQuery, error) {
return NewIntQuery("state", int(value), IntEquals)
}
func newActionIDSearchQuery(id string) (SearchQuery, error) {
return NewTextQuery("id", id, TextEquals)
}