<commit_before>package authz

import (
	"fmt"

	"git.dsr-corporation.com/zb-ledger/zb-ledger/x/authz/internal/keeper"
	"git.dsr-corporation.com/zb-ledger/zb-ledger/x/authz/internal/types"
	sdk "github.com/cosmos/cosmos-sdk/types"
)

func NewHandler(keeper keeper.Keeper) sdk.Handler {
	return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
		switch msg := msg.(type) {
		case types.MsgAssignRole:
			return handleMsgAssignRole(ctx, keeper, msg)
		case types.MsgRevokeRole:
			return handleMsgRevokeRole(ctx, keeper, msg)
		default:
			errMsg := fmt.Sprintf("unrecognized nameservice Msg type: %v", msg.Type())
			return sdk.ErrUnknownRequest(errMsg).Result()
		}
	}
}

func handleMsgAssignRole(ctx sdk.Context, keeper keeper.Keeper, msg types.MsgAssignRole) sdk.Result {
	if !keeper.HasRole(ctx, msg.Signer, Administrator) {
		return sdk.ErrUnauthorized("you are not authorized to perform this action").Result()
	}

	keeper.AssignRole(ctx, msg.Address, msg.Role)

	return sdk.Result{}
}

func handleMsgRevokeRole(ctx sdk.Context, keeper keeper.Keeper, msg types.MsgRevokeRole) sdk.Result {
	if !keeper.HasRole(ctx, msg.Signer, Administrator) {
		return sdk.ErrUnauthorized("you are not authorized to perform this action").Result()
	}

	if !keeper.HasRole(ctx, msg.Address, msg.Role) {
		return sdk.ErrUnauthorized(fmt.Sprintf("account %s doesn't have role %s", msg.Address.String(), msg.Role)).Result()
	}

	if keeper.CountAccounts(ctx, Administrator) < 2 {
		return sdk.ErrUnauthorized("there must be at least one administrator").Result()
	}

	keeper.RevokeRole(ctx, msg.Address, msg.Role)

	return sdk.Result{}
}<commit_msg>Fixed the bug when administrator can't revoke the last manufacturer role<commit_after>package authz

import (
	"fmt"

	"git.dsr-corporation.com/zb-ledger/zb-ledger/x/authz/internal/keeper"
	"git.dsr-corporation.com/zb-ledger/zb-ledger/x/authz/internal/types"
	sdk "github.com/cosmos/cosmos-sdk/types"
)

func NewHandler(keeper keeper.Keeper) sdk.Handler {
	return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
		switch msg := msg.(type) {
		case types.MsgAssignRole:
			return handleMsgAssignRole(ctx, keeper, msg)
		case types.MsgRevokeRole:
			return handleMsgRevokeRole(ctx, keeper, msg)
		default:
			errMsg := fmt.Sprintf("unrecognized nameservice Msg type: %v", msg.Type())
			return sdk.ErrUnknownRequest(errMsg).Result()
		}
	}
}

func handleMsgAssignRole(ctx sdk.Context, keeper keeper.Keeper, msg types.MsgAssignRole) sdk.Result {
	if !keeper.HasRole(ctx, msg.Signer, Administrator) {
		return sdk.ErrUnauthorized("you are not authorized to perform this action").Result()
	}

	keeper.AssignRole(ctx, msg.Address, msg.Role)

	return sdk.Result{}
}

func handleMsgRevokeRole(ctx sdk.Context, keeper keeper.Keeper, msg types.MsgRevokeRole) sdk.Result {
	if !keeper.HasRole(ctx, msg.Signer, Administrator) {
		return sdk.ErrUnauthorized("you are not authorized to perform this action").Result()
	}

	if !keeper.HasRole(ctx, msg.Address, msg.Role) {
		return sdk.ErrUnauthorized(fmt.Sprintf("account %s doesn't have role %s", msg.Address.String(), msg.Role)).Result()
	}

	if msg.Role == Administrator && keeper.CountAccounts(ctx, Administrator) < 2 {
		return sdk.ErrUnauthorized("there must be at least one administrator").Result()
	}

	keeper.RevokeRole(ctx, msg.Address, msg.Role)

	return sdk.Result{}
}
{'fraction_non_alphanumeric': 0.12926391382405744, 'fraction_numerical': 0.0005984440454817474, 'mean_word_length': 4.88556338028169, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1025624', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1131, 'n_words': 252}