github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/auth/helpers.go (about)

     1  package auth
     2  
     3  import (
     4  	"github.com/ngocphuongnb/tetua/app/cache"
     5  	"github.com/ngocphuongnb/tetua/app/entities"
     6  	"github.com/ngocphuongnb/tetua/app/repositories"
     7  	"github.com/ngocphuongnb/tetua/app/server"
     8  )
     9  
    10  func GetRolePermissions(roleID int) *entities.RolePermissions {
    11  	for _, rolePermission := range cache.RolesPermissions {
    12  		if rolePermission.RoleID == roleID {
    13  			return rolePermission
    14  		}
    15  	}
    16  
    17  	return &entities.RolePermissions{
    18  		RoleID:      roleID,
    19  		Permissions: []*entities.PermissionValue{},
    20  	}
    21  }
    22  
    23  func GetRolePermission(roleID int, action string) *entities.PermissionValue {
    24  	rolePermissions := GetRolePermissions(roleID)
    25  
    26  	for _, permission := range rolePermissions.Permissions {
    27  		if permission.Action == action {
    28  			return permission
    29  		}
    30  	}
    31  
    32  	return &entities.PermissionValue{}
    33  }
    34  
    35  func GetRolesFromIDs(IDs []int) []*entities.Role {
    36  	result := []*entities.Role{}
    37  
    38  	for _, role := range cache.Roles {
    39  		for _, id := range IDs {
    40  			if role.ID == id {
    41  				result = append(result, role)
    42  			}
    43  		}
    44  	}
    45  
    46  	return result
    47  }
    48  
    49  func GetFile(c server.Context) error {
    50  	fileID := c.ParamInt("id")
    51  	file, err := repositories.File.ByID(c.Context(), fileID)
    52  
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	c.Locals("file", file)
    58  
    59  	return nil
    60  }
    61  
    62  func GetPost(c server.Context) error {
    63  	postIDParam := c.Param("id")
    64  
    65  	if postIDParam == "new" {
    66  		return nil
    67  	}
    68  
    69  	post, err := repositories.Post.ByID(c.Context(), c.ParamInt("id"))
    70  
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	c.Post(post)
    76  
    77  	return nil
    78  }
    79  
    80  func GetComment(c server.Context) error {
    81  	commentIDParam := c.Param("id")
    82  
    83  	if commentIDParam == "new" {
    84  		return nil
    85  	}
    86  
    87  	comment, err := repositories.Comment.ByID(c.Context(), c.ParamInt("id"))
    88  
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	c.Locals("comment", comment)
    94  
    95  	return nil
    96  }
    97  
    98  func FileOwnerCheck(c server.Context) bool {
    99  	if c.Param("id") == "new" {
   100  		return true
   101  	}
   102  
   103  	user := c.User()
   104  	file, ok := c.Locals("file").(*entities.File)
   105  
   106  	if !ok {
   107  		return false
   108  	}
   109  
   110  	if user == nil || file == nil {
   111  		return false
   112  	}
   113  
   114  	if file.UserID != user.ID {
   115  		return false
   116  	}
   117  
   118  	return true
   119  }
   120  
   121  func PostOwnerCheck(c server.Context) bool {
   122  	if c.Param("id") == "new" {
   123  		return true
   124  	}
   125  
   126  	user := c.User()
   127  	post := c.Post()
   128  
   129  	if user == nil || post == nil {
   130  		return false
   131  	}
   132  
   133  	if post.UserID != user.ID {
   134  		return false
   135  	}
   136  
   137  	c.Post(post)
   138  	return true
   139  }
   140  
   141  func CommentOwnerCheck(c server.Context) bool {
   142  	if c.Param("id") == "new" {
   143  		return true
   144  	}
   145  
   146  	user := c.User()
   147  	comment, ok := c.Locals("comment").(*entities.Comment)
   148  
   149  	if !ok {
   150  		return false
   151  	}
   152  
   153  	if user == nil || comment == nil {
   154  		return false
   155  	}
   156  
   157  	if comment.UserID != user.ID {
   158  		return false
   159  	}
   160  
   161  	return true
   162  }
   163  
   164  func AllowLoggedInUser(c server.Context) bool {
   165  	user := c.User()
   166  	if user == nil || user.ID == 0 {
   167  		return false
   168  	}
   169  
   170  	return true
   171  }
   172  
   173  func AllowAll(c server.Context) bool {
   174  	return true
   175  }
   176  
   177  func AllowNone(c server.Context) bool {
   178  	return false
   179  }