adminmutator

package
v0.0.0-...-4334ecd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChangeUserPassword = &graphql.Field{
	Name:        "ChangeUserPassword",
	Description: "",
	Type:        kind.GQLProposalResponseType,
	Args: graphql.FieldConfigArgument{
		"username": &graphql.ArgumentConfig{
			Description: "Username",
			Type:        graphql.NewNonNull(graphql.String),
		},
		"password": &graphql.ArgumentConfig{
			Description: "Password",
			Type:        graphql.NewNonNull(graphql.String),
		},
	},

	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		username := p.Args["username"].(string)
		if !utility.IsUsernameValid(username) {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidUsername.Error())
		}

		admin, ok := p.Source.(*flamed.Admin)
		if !ok {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidSourceType.Error())
		}

		user, err := admin.GetUser(username)

		if err != nil {
			return nil, gqlerrors.NewFormattedError(err.Error())
		}

		user.UpdatedAt = uint64(time.Now().UnixNano())
		password := p.Args["password"].(string)

		if len(password) == 0 {
			return nil, gqlerrors.NewFormattedError(x.ErrPasswordCanNotBeEmpty.Error())
		}

		pha := variable.DefaultPasswordHashAlgorithmFactory
		if !pha.IsAlgorithmAvailable(variable.DefaultPasswordHashAlgorithm) {
			logger.L("app").Error(variable.DefaultPasswordHashAlgorithm +
				" password hash algorithm is to available")
			return nil, gqlerrors.NewFormattedError(x.ErrPasswordHashAlgorithmIsNotAvailable.Error())
		}

		encoded, err := pha.MakePassword(password,
			crypto.GetRandomString(12),
			variable.DefaultPasswordHashAlgorithm)

		if err != nil {
			logger.L("app").Error("make password returned error", zap.Error(err))
			return nil, gqlerrors.NewFormattedError(x.ErrFailedToGeneratePassword.Error())
		}
		user.Password = encoded

		pr, err := admin.UpsertUser(user)
		if err != nil {
			return nil, err
		}

		return pr, nil
	},
}
View Source
var DeleteAccessControl = &graphql.Field{
	Name:        "DeleteIndexMeta",
	Description: "",
	Type:        kind.GQLProposalResponseType,
	Args: graphql.FieldConfigArgument{
		"username": &graphql.ArgumentConfig{
			Description: "Username",
			Type:        graphql.NewNonNull(graphql.String),
		},
		"namespace": &graphql.ArgumentConfig{
			Description: "Namespace",
			Type:        graphql.NewNonNull(graphql.String),
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		username := p.Args["username"].(string)
		namespace := []byte(p.Args["namespace"].(string))
		admin, ok := p.Source.(*flamed.Admin)
		if !ok {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidSourceType.Error())
		}

		pr, err := admin.DeleteAccessControl(namespace, username)
		if err != nil {
			return nil, err
		}

		return pr, nil
	},
}
View Source
var DeleteIndexMeta = &graphql.Field{
	Name:        "DeleteIndexMeta",
	Description: "",
	Type:        kind.GQLProposalResponseType,
	Args: graphql.FieldConfigArgument{
		"namespace": &graphql.ArgumentConfig{
			Description: "Namespace",
			Type:        graphql.NewNonNull(graphql.String),
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		namespace := []byte(p.Args["namespace"].(string))
		admin, ok := p.Source.(*flamed.Admin)
		if !ok {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidSourceType.Error())
		}

		pr, err := admin.DeleteIndexMeta(namespace)
		if err != nil {
			return nil, err
		}

		return pr, nil
	},
}
View Source
var DeleteUser = &graphql.Field{
	Name:        "DeleteUser",
	Description: "",
	Type:        kind.GQLProposalResponseType,
	Args: graphql.FieldConfigArgument{
		"username": &graphql.ArgumentConfig{
			Description: "Username",
			Type:        graphql.NewNonNull(graphql.String),
		},
	},
	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		username := p.Args["username"].(string)
		admin, ok := p.Source.(*flamed.Admin)

		if username == "admin" {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidOperation.Error())
		}

		if !ok {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidSourceType.Error())
		}

		pr, err := admin.DeleteUser(username)
		if err != nil {
			return nil, err
		}

		return pr, nil
	},
}
View Source
var GQLAdminMutatorType = graphql.NewObject(graphql.ObjectConfig{
	Name:        "AdminMutator",
	Description: "`AdminMutator`",
	Fields: graphql.Fields{
		"upsertAccessControl": UpsertAccessControl,
		"deleteAccessControl": DeleteIndexMeta,
		"deleteUser":          DeleteUser,
		"upsertUser":          UpsertUser,
		"updateUser":          UpdateUser,
		"changeUserPassword":  ChangeUserPassword,

		"upsertIndexMeta": UpsertIndexMeta,
		"deleteIndexMeta": DeleteIndexMeta,
	},
})
View Source
var UpdateUser = &graphql.Field{
	Name:        "UpdateUser",
	Description: "",
	Type:        kind.GQLProposalResponseType,
	Args: graphql.FieldConfigArgument{
		"userType": &graphql.ArgumentConfig{
			Description: "User type",
			Type:        kind.GQLUserTypeEnum,
		},
		"roles": &graphql.ArgumentConfig{
			Description: "Roles",
			Type:        graphql.String,
		},
		"username": &graphql.ArgumentConfig{
			Description: "Username",
			Type:        graphql.NewNonNull(graphql.String),
		},
		"password": &graphql.ArgumentConfig{
			Description: "Password",
			Type:        graphql.String,
		},
		"data": &graphql.ArgumentConfig{
			Description: "Data in base64 encoded string",
			Type:        graphql.String,
		},
		"meta": &graphql.ArgumentConfig{
			Description: "Meta in base64 encoded string",
			Type:        graphql.String,
		},
	},

	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		username := p.Args["username"].(string)
		if !utility.IsUsernameValid(username) {
			return nil, gqlerrors.NewFormattedError("invalid username")
		}

		if username == "admin" {
			if p.Args["userType"] != nil {
				if pb.UserType(p.Args["userType"].(int)) != pb.UserType_SUPER_USER {
					return nil, gqlerrors.NewFormattedError(x.ErrInvalidOperation.Error())
				}
			}
		}

		admin, ok := p.Source.(*flamed.Admin)
		if !ok {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidSourceType.Error())
		}

		user, err := admin.GetUser(username)

		if err != nil {
			return nil, gqlerrors.NewFormattedError(err.Error())
		}

		user.UpdatedAt = uint64(time.Now().UnixNano())

		if p.Args["password"] != nil {
			password := p.Args["password"].(string)

			pha := variable.DefaultPasswordHashAlgorithmFactory
			if !pha.IsAlgorithmAvailable(variable.DefaultPasswordHashAlgorithm) {
				logger.L("app").Error(variable.DefaultPasswordHashAlgorithm +
					" password hash algorithm is to available")
				return nil, gqlerrors.NewFormattedError(x.ErrPasswordHashAlgorithmIsNotAvailable.Error())
			}

			encoded, err := pha.MakePassword(password,
				crypto.GetRandomString(12),
				variable.DefaultPasswordHashAlgorithm)

			if err != nil {
				logger.L("app").Error("make password returned error", zap.Error(err))
				return nil, gqlerrors.NewFormattedError(x.ErrFailedToGeneratePassword.Error())
			}
			user.Password = encoded
		}

		if p.Args["userType"] != nil {
			user.UserType = pb.UserType(p.Args["userType"].(int))
		}

		if p.Args["roles"] != nil {
			user.Roles = p.Args["roles"].(string)
		}

		if p.Args["data"] != nil {
			data, e := base64.StdEncoding.DecodeString(p.Args["data"].(string))
			if e == nil {
				user.Data = data
			}
		}

		if p.Args["meta"] != nil {
			meta, e := base64.StdEncoding.DecodeString(p.Args["meta"].(string))
			if e == nil {
				user.Meta = meta
			}
		}

		pr, err := admin.UpsertUser(user)
		if err != nil {
			return nil, err
		}

		return pr, nil
	},
}
View Source
var UpsertAccessControl = &graphql.Field{
	Name:        "UpsertAccessControl",
	Description: "",
	Type:        kind.GQLProposalResponseType,
	Args: graphql.FieldConfigArgument{
		"username": &graphql.ArgumentConfig{
			Description: "Username",
			Type:        graphql.NewNonNull(graphql.String),
		},
		"namespace": &graphql.ArgumentConfig{
			Description: "Namespace",
			Type:        graphql.NewNonNull(graphql.String),
		},

		"permission": &graphql.ArgumentConfig{
			Description: "Access permission",
			Type:        graphql.NewNonNull(kind.GQLPermissionInputType),
		},
		"data": &graphql.ArgumentConfig{
			Description: "Data in base64 encoded string",
			Type:        graphql.String,
		},
		"meta": &graphql.ArgumentConfig{
			Description: "Meta in base64 encoded string",
			Type:        graphql.String,
		},
	},

	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		username := p.Args["username"].(string)
		namespace := []byte(p.Args["namespace"].(string))

		permission := p.Args["permission"].(map[string]interface{})
		readPermission := permission["read"].(bool)
		writePermission := permission["write"].(bool)
		updatePermission := permission["update"].(bool)
		deletePermission := permission["delete"].(bool)

		globalSearchPermission := permission["globalSearch"].(bool)
		globalIteratePermission := permission["globalIterate"].(bool)
		globalRetrievePermission := permission["globalRetrieve"].(bool)
		globalCRUDPermission := permission["globalCRUD"].(bool)

		if !bytes.Equal(namespace, []byte("*")) {
			if !utility.IsNamespaceValid(namespace) {
				return nil, gqlerrors.NewFormattedError(x.ErrInvalidNamespace.Error())
			}
		}

		if !utility.IsUsernameValid(username) {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidUsername.Error())
		}

		admin, ok := p.Source.(*flamed.Admin)
		if !ok {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidSourceType.Error())
		}

		permissionNumber := utility.NewPermission(
			readPermission,
			writePermission,
			updatePermission,
			deletePermission,
			globalSearchPermission,
			globalIteratePermission,
			globalRetrievePermission,
			globalCRUDPermission)

		accessControl := &pb.AccessControl{
			Username:   username,
			Namespace:  namespace,
			Permission: permissionNumber,
			CreatedAt:  uint64(time.Now().UnixNano()),
			UpdatedAt:  uint64(time.Now().UnixNano()),
			Data:       nil,
			Meta:       nil,
		}

		if p.Args["data"] != nil {
			data, e := base64.StdEncoding.DecodeString(p.Args["data"].(string))
			if e == nil {
				accessControl.Data = data
			}
		}

		if p.Args["meta"] != nil {
			meta, e := base64.StdEncoding.DecodeString(p.Args["meta"].(string))
			if e == nil {
				accessControl.Meta = meta
			}
		}

		pr, err := admin.UpsertAccessControl(accessControl)
		if err != nil {
			return nil, err
		}

		return pr, nil
	},
}
View Source
var UpsertIndexMeta = &graphql.Field{
	Name:        "UpsertIndexMeta",
	Description: "Upsert index meta",
	Type:        kind.GQLProposalResponseType,
	Args: graphql.FieldConfigArgument{
		"input": &graphql.ArgumentConfig{
			Description: "Index meta input",
			Type:        graphql.NewNonNull(kind.GQLIndexMetaInputType),
		},
	},

	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		input := p.Args["input"].(map[string]interface{})
		admin, ok := p.Source.(*flamed.Admin)
		if !ok {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidSourceType.Error())
		}

		indexMeta := utility.BuildIndexMetaFromMap(input)
		indexMeta.CreatedAt = uint64(time.Now().UnixNano())
		indexMeta.UpdatedAt = uint64(time.Now().UnixNano())

		pr, err := admin.UpsertIndexMeta(indexMeta)
		if err != nil {
			return nil, err
		}

		return pr, nil
	},
}
View Source
var UpsertUser = &graphql.Field{
	Name:        "UpsertUser",
	Description: "",
	Type:        kind.GQLProposalResponseType,
	Args: graphql.FieldConfigArgument{
		"userType": &graphql.ArgumentConfig{
			Description: "User type",
			Type:        kind.GQLUserTypeEnum,
		},
		"roles": &graphql.ArgumentConfig{
			Description: "Roles",
			Type:        graphql.String,
		},
		"username": &graphql.ArgumentConfig{
			Description: "Username",
			Type:        graphql.NewNonNull(graphql.String),
		},
		"password": &graphql.ArgumentConfig{
			Description: "Password",
			Type:        graphql.NewNonNull(graphql.String),
		},
		"data": &graphql.ArgumentConfig{
			Description: "Data in base64 encoded string",
			Type:        graphql.String,
		},
		"meta": &graphql.ArgumentConfig{
			Description: "Meta in base64 encoded string",
			Type:        graphql.String,
		},
	},

	Resolve: func(p graphql.ResolveParams) (interface{}, error) {
		username := p.Args["username"].(string)
		password := p.Args["password"].(string)
		if !utility.IsUsernameValid(username) {
			return nil, gqlerrors.NewFormattedError("invalid username")
		}

		if username == "admin" {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidOperation.Error())
		}

		admin, ok := p.Source.(*flamed.Admin)
		if !ok {
			return nil, gqlerrors.NewFormattedError(x.ErrInvalidSourceType.Error())
		}

		pha := variable.DefaultPasswordHashAlgorithmFactory
		if !pha.IsAlgorithmAvailable(variable.DefaultPasswordHashAlgorithm) {
			logger.L("app").Error(variable.DefaultPasswordHashAlgorithm +
				" password hash algorithm is to available")
			return nil, gqlerrors.NewFormattedError(x.ErrPasswordHashAlgorithmIsNotAvailable.Error())
		}

		encoded, err := pha.MakePassword(password,
			crypto.GetRandomString(12),
			variable.DefaultPasswordHashAlgorithm)

		if err != nil {
			logger.L("app").Error("make password returned error", zap.Error(err))
			return nil, gqlerrors.NewFormattedError(x.ErrFailedToGeneratePassword.Error())
		}

		user := &pb.User{
			UserType:  pb.UserType_NORMAL_USER,
			Roles:     "",
			Username:  username,
			Password:  encoded,
			CreatedAt: uint64(time.Now().UnixNano()),
			UpdatedAt: uint64(time.Now().UnixNano()),
			Data:      nil,
			Meta:      nil,
		}

		if p.Args["userType"] != nil {
			user.UserType = pb.UserType(p.Args["userType"].(int))
		}

		if p.Args["roles"] != nil {
			user.Roles = p.Args["roles"].(string)
		}

		if p.Args["data"] != nil {
			data, e := base64.StdEncoding.DecodeString(p.Args["data"].(string))
			if e == nil {
				user.Data = data
			}
		}

		if p.Args["meta"] != nil {
			meta, e := base64.StdEncoding.DecodeString(p.Args["meta"].(string))
			if e == nil {
				user.Meta = meta
			}
		}

		pr, err := admin.UpsertUser(user)
		if err != nil {
			return nil, err
		}

		return pr, nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL