Пример #1
0
//=====================================================================
func RolePerModuleAdd(roleID, roleCode, moduleID string) ([]modules.RolePermission, error) {
	list := []modules.RolePermission{}
	per, err := dal.RolePerCheck(moduleID, roleID, true)
	if err != nil {
		return nil, err
	}
	if per != nil && per.IsRef {
		err = dal.RolePerIsRefUpdate(per.PermissionId, false)
		if err != nil {
			return nil, err
		}
	} else if per == nil {
		mod, modErr := dal.ModuleInfo(bson.ObjectIdHex(moduleID))
		if modErr != nil {
			return nil, modErr
		}
		per = &modules.RolePermission{}
		per.PermissionId = bson.NewObjectId()
		per.RoleID = roleID
		per.RoleCode = roleCode
		per.ModuleID = mod.ID.Hex()
		per.ModuleCode = mod.Code
		per.IsModule = true
		per.IsRef = false
		parentMod, parErr := dal.ModuleInfo(bson.ObjectIdHex(mod.ParentID))
		if parErr != nil {
			return nil, parErr
		} else if parentMod.Code != "system" {
			parentPer, perErr := dal.RolePerCheck(parentMod.ID.Hex(), roleID, true)
			if perErr != nil {
				return nil, perErr
			}
			if parentPer == nil {
				parentPer.PermissionId = bson.NewObjectId()
				parentPer.RoleID = roleID
				parentPer.RoleCode = roleCode
				parentPer.ModuleID = parentMod.ID.Hex()
				parentPer.ModuleCode = parentMod.Code
				parentPer.IsModule = true
				parentPer.IsRef = true
				per.ParentPerID = parentPer.PermissionId.Hex()
			} else {
				per.ParentPerID = parentPer.PermissionId.Hex()
			}
			err = rolePerAddCheck(roleID, roleCode, parentPer, list)
		}
	}
	list = append(list, *per)
	if err == nil {
		err = dal.RolePersInsert(list)
	}
	return list, err
}
Пример #2
0
func rolePerAddCheck(roleID, roleCode string, per *modules.RolePermission, parentPers []modules.RolePermission) error {

	parentMod, parErr := dal.ModuleInfo(bson.ObjectIdHex(per.ModuleID))
	if parErr != nil {
		return parErr
	} else if parentMod.Code == "system" {
		return nil
	}
	parentPer, perErr := dal.RolePerCheck(parentMod.ID.Hex(), roleID, true)
	if perErr != nil {
		return perErr
	}
	if parentPer == nil {
		parentPer = &modules.RolePermission{}
		parentPer.PermissionId = bson.NewObjectId()
		parentPer.RoleID = roleID
		parentPer.RoleCode = roleCode
		parentPer.ModuleID = parentMod.ID.Hex()
		parentPer.ModuleCode = parentMod.Code
		parentPer.IsModule = true
		parentPer.IsRef = true
		per.ParentPerID = parentPer.PermissionId.Hex()
	} else {
		per.ParentPerID = parentPer.PermissionId.Hex()
	}
	parentPers = append(parentPers, *per)
	return rolePerAddCheck(roleID, roleCode, parentPer, parentPers)
}
Пример #3
0
func ModuleInfo(id bson.ObjectId) (*modules.Module, error) {
	return dal.ModuleInfo(id)
}