2020-03-24 09:14:39 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-15 14:50:09 +00:00
|
|
|
|
2020-05-12 04:30:53 +00:00
|
|
|
"github.com/caos/zitadel/internal/api"
|
2020-06-11 11:27:25 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/auth"
|
2020-05-12 04:30:53 +00:00
|
|
|
grpc_util "github.com/caos/zitadel/internal/api/grpc"
|
2020-03-24 09:14:39 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) CreateProject(ctx context.Context, in *ProjectCreateRequest) (*Project, error) {
|
2020-04-07 11:23:04 +00:00
|
|
|
project, err := s.project.CreateProject(ctx, in.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectFromModel(project), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
func (s *Server) UpdateProject(ctx context.Context, in *ProjectUpdateRequest) (*Project, error) {
|
2020-06-09 12:41:09 +00:00
|
|
|
if s.IsZitadel(ctx, in.Id) {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "GRPC-SFH8d", "Zitadel Project should not be updated")
|
|
|
|
}
|
2020-04-07 11:23:04 +00:00
|
|
|
project, err := s.project.UpdateProject(ctx, projectUpdateToModel(in))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectFromModel(project), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
func (s *Server) DeactivateProject(ctx context.Context, in *ProjectID) (*Project, error) {
|
2020-06-09 12:41:09 +00:00
|
|
|
if s.IsZitadel(ctx, in.Id) {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "GRPC-PS9cs", "Zitadel Project should not be deactivated")
|
|
|
|
}
|
2020-04-07 11:23:04 +00:00
|
|
|
project, err := s.project.DeactivateProject(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectFromModel(project), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
func (s *Server) ReactivateProject(ctx context.Context, in *ProjectID) (*Project, error) {
|
2020-06-09 12:41:09 +00:00
|
|
|
if s.IsZitadel(ctx, in.Id) {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "GRPC-LSpe2", "Zitadel Project should not be reactivated")
|
|
|
|
}
|
2020-04-07 11:23:04 +00:00
|
|
|
project, err := s.project.ReactivateProject(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectFromModel(project), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (s *Server) SearchProjects(ctx context.Context, in *ProjectSearchRequest) (*ProjectSearchResponse, error) {
|
|
|
|
request := projectSearchRequestsToModel(in)
|
|
|
|
request.AppendMyResourceOwnerQuery(grpc_util.GetHeader(ctx, api.ZitadelOrgID))
|
|
|
|
response, err := s.project.SearchProjects(ctx, request)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-15 12:50:39 +00:00
|
|
|
return projectSearchResponseFromModel(response), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ProjectByID(ctx context.Context, id *ProjectID) (*Project, error) {
|
2020-04-07 11:23:04 +00:00
|
|
|
project, err := s.project.ProjectByID(ctx, id.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectFromModel(project), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (s *Server) SearchGrantedProjects(ctx context.Context, in *GrantedProjectSearchRequest) (*ProjectGrantSearchResponse, error) {
|
|
|
|
request := grantedProjectSearchRequestsToModel(in)
|
|
|
|
request.AppendMyOrgQuery(grpc_util.GetHeader(ctx, api.ZitadelOrgID))
|
|
|
|
response, err := s.project.SearchProjectGrants(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectGrantSearchResponseFromModel(response), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetGrantedProjectByID(ctx context.Context, in *ProjectGrantID) (*ProjectGrantView, error) {
|
|
|
|
project, err := s.project.ProjectGrantViewByID(ctx, in.Id)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-15 12:50:39 +00:00
|
|
|
return projectGrantFromGrantedProjectModel(project), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
func (s *Server) AddProjectRole(ctx context.Context, in *ProjectRoleAdd) (*ProjectRole, error) {
|
2020-06-09 12:41:09 +00:00
|
|
|
if s.IsZitadel(ctx, in.Id) {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "GRPC-PS9cs", "Zitadel Project should not get new role")
|
|
|
|
}
|
2020-04-21 15:00:32 +00:00
|
|
|
role, err := s.project.AddProjectRole(ctx, projectRoleAddToModel(in))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectRoleFromModel(role), nil
|
|
|
|
}
|
|
|
|
func (s *Server) ChangeProjectRole(ctx context.Context, in *ProjectRoleChange) (*ProjectRole, error) {
|
2020-06-09 12:41:09 +00:00
|
|
|
if s.IsZitadel(ctx, in.Id) {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "GRPC-LASj8", "Zitadel Project should not change roles")
|
|
|
|
}
|
2020-04-21 15:00:32 +00:00
|
|
|
role, err := s.project.ChangeProjectRole(ctx, projectRoleChangeToModel(in))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectRoleFromModel(role), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
2020-04-21 15:00:32 +00:00
|
|
|
|
2020-03-24 09:14:39 +00:00
|
|
|
func (s *Server) RemoveProjectRole(ctx context.Context, in *ProjectRoleRemove) (*empty.Empty, error) {
|
2020-06-09 12:41:09 +00:00
|
|
|
if s.IsZitadel(ctx, in.Id) {
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "GRPC-Psn7s", "do not remove roles from Zitadel Project")
|
|
|
|
}
|
2020-04-21 15:00:32 +00:00
|
|
|
err := s.project.RemoveProjectRole(ctx, in.Id, in.Key)
|
|
|
|
return &empty.Empty{}, err
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SearchProjectRoles(ctx context.Context, in *ProjectRoleSearchRequest) (*ProjectRoleSearchResponse, error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
request := projectRoleSearchRequestsToModel(in)
|
2020-06-11 11:27:25 +00:00
|
|
|
request.AppendMyOrgQuery(auth.GetCtxData(ctx).OrgID)
|
|
|
|
request.AppendProjectQuery(in.ProjectId)
|
2020-05-11 10:16:29 +00:00
|
|
|
response, err := s.project.SearchProjectRoles(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectRoleSearchResponseFromModel(response), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ProjectChanges(ctx context.Context, changesRequest *ChangeRequest) (*Changes, error) {
|
2020-06-15 14:50:09 +00:00
|
|
|
response, err := s.project.ProjectChanges(ctx, changesRequest.Id, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projectChangesToResponse(response, changesRequest.GetSequenceOffset(), changesRequest.GetLimit()), nil
|
2020-03-24 09:14:39 +00:00
|
|
|
}
|
2020-06-09 12:41:09 +00:00
|
|
|
|
|
|
|
func (s *Server) IsZitadel(ctx context.Context, projectID string) bool {
|
|
|
|
iam, err := s.iam.IamByID(ctx, s.systemDefaults.IamID)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if iam.IamProjectID == projectID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|