github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/web/manage/role/role.go (about)

     1  package managerole
     2  
     3  import (
     4  	"net/http"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/ngocphuongnb/tetua/app/auth"
     9  	"github.com/ngocphuongnb/tetua/app/cache"
    10  	"github.com/ngocphuongnb/tetua/app/entities"
    11  	"github.com/ngocphuongnb/tetua/app/repositories"
    12  	"github.com/ngocphuongnb/tetua/app/server"
    13  	"github.com/ngocphuongnb/tetua/app/utils"
    14  	"github.com/ngocphuongnb/tetua/views"
    15  )
    16  
    17  func Index(c server.Context) (err error) {
    18  	status := http.StatusOK
    19  	roles, err := repositories.Role.All(c.Context())
    20  	c.Meta().Title = "Manage roles"
    21  
    22  	if err != nil {
    23  		status = http.StatusBadRequest
    24  		c.WithError("Load all roles error", err)
    25  	}
    26  
    27  	return c.Status(status).Render(views.ManageRoleIndex(roles))
    28  }
    29  
    30  func Compose(c server.Context) (err error) {
    31  	return composeView(c, &entities.RoleMutation{}, false)
    32  }
    33  
    34  func Save(c server.Context) (err error) {
    35  	var role *entities.Role
    36  	data := getRoleSaveData(c)
    37  
    38  	if role, err = getProcessingRole(c); err != nil {
    39  		c.WithError("Query editting role error", err)
    40  	}
    41  
    42  	if c.Messages().Length() > 0 {
    43  		return composeView(c, data, true)
    44  	}
    45  
    46  	role.Root = data.Root
    47  	role.Name = data.Name
    48  	role.Description = data.Description
    49  
    50  	if role.ID > 0 {
    51  		role, err = repositories.Role.Update(c.Context(), role)
    52  	} else {
    53  		role, err = repositories.Role.Create(c.Context(), role)
    54  	}
    55  
    56  	if err != nil {
    57  		c.WithError("Error saving role", err)
    58  		return composeView(c, data, true)
    59  	}
    60  
    61  	if !role.Root {
    62  		if err := repositories.Role.SetPermissions(c.Context(), role.ID, data.Permissions); err != nil {
    63  			c.WithError("Error saving role", err)
    64  			return composeView(c, data, true)
    65  		}
    66  	}
    67  
    68  	if err := cache.CachePermissions(c.Context()); err != nil {
    69  		c.WithError("Error caching permissions", err)
    70  		return composeView(c, data, true)
    71  	}
    72  	return c.Redirect("/manage/roles/" + strconv.Itoa(role.ID))
    73  }
    74  
    75  func Delete(c server.Context) error {
    76  	role, err := getProcessingRole(c)
    77  
    78  	if role.ID < 4 {
    79  		return c.Status(http.StatusBadRequest).SendString("Error deleting role")
    80  	}
    81  
    82  	if err != nil {
    83  		c.Logger().Error("Error deleting role", err)
    84  		return c.Status(http.StatusBadRequest).SendString("Error deleting role")
    85  	}
    86  
    87  	if err := repositories.Role.DeleteByID(c.Context(), role.ID); err != nil {
    88  		c.Logger().Error("Error deleting role", err)
    89  		return c.Status(http.StatusBadRequest).SendString("Error deleting role")
    90  	}
    91  
    92  	return c.Status(http.StatusOK).SendString("Success")
    93  }
    94  
    95  func getProcessingRole(c server.Context) (role *entities.Role, err error) {
    96  	if c.Param("id") == "new" {
    97  		return &entities.Role{}, nil
    98  	}
    99  
   100  	return repositories.Role.ByID(c.Context(), c.ParamInt("id"))
   101  }
   102  
   103  func composeView(c server.Context, data *entities.RoleMutation, isSave bool) (err error) {
   104  	role, err := getProcessingRole(c)
   105  	c.Meta().Title = "Create Role"
   106  
   107  	if err != nil {
   108  		c.WithError("Query editting role error", err)
   109  	} else if !isSave {
   110  		data.Root = role.Root
   111  		data.Name = role.Name
   112  		data.Description = role.Description
   113  	}
   114  
   115  	if role.ID > 0 {
   116  		c.Meta().Title = "Edit Role: " + role.Name
   117  	}
   118  
   119  	var rolePermissions []*entities.PermissionValue
   120  	var rolePermissionsByActions = map[string]string{}
   121  
   122  	for _, permission := range role.Permissions {
   123  		rolePermissionsByActions[permission.Action] = permission.Value
   124  	}
   125  
   126  	for _, permissionValue := range auth.ActionConfigs {
   127  		if value, ok := rolePermissionsByActions[permissionValue.Action]; ok {
   128  			rolePermissions = append(rolePermissions, &entities.PermissionValue{
   129  				Action: permissionValue.Action,
   130  				Value:  entities.GetPermTypeValue(value),
   131  			})
   132  		} else {
   133  			rolePermissions = append(rolePermissions, &entities.PermissionValue{
   134  				Action: permissionValue.Action,
   135  				Value:  entities.PermType(permissionValue.DefaultValue),
   136  			})
   137  		}
   138  	}
   139  
   140  	return c.Render(views.ManageRoleCompose(role.ID, data, rolePermissions))
   141  }
   142  
   143  func getRoleSaveData(c server.Context) *entities.RoleMutation {
   144  	data := &entities.RoleMutation{}
   145  	if err := c.BodyParser(data); err != nil {
   146  		c.WithError("Error parsing body", err)
   147  		return data
   148  	}
   149  
   150  	data.Name = utils.SanitizePlainText(strings.TrimSpace(data.Name))
   151  	data.Description = utils.SanitizePlainText(strings.TrimSpace(data.Description))
   152  
   153  	if data.Name == "" || len(data.Name) > 250 {
   154  		c.Messages().AppendError("Name is required and can't be more than 250 characters")
   155  	}
   156  
   157  	return data
   158  }