Skip to content

Instantly share code, notes, and snippets.

@IlyaGulya
Last active August 10, 2018 09:20
Show Gist options
  • Save IlyaGulya/3db22a86a5a4871067df27f895114e7d to your computer and use it in GitHub Desktop.
Save IlyaGulya/3db22a86a5a4871067df27f895114e7d to your computer and use it in GitHub Desktop.
@JvmStatic
fun <T : RealmObject> filterAccess(query: RealmQuery<T>): RealmQuery<T> {
val roles = DCUserManager.getRoles().toTypedArray()
fun RealmQuery<T>.operationTypeIs(operationType: String) {
equalTo(RealmUtil.getField(ModelWithType.FIELD_ACCESS, AccessModel.FIELD_OPERATION), operationType)
}
fun RealmQuery<T>.hasAnyOfRoles(roles: Array<Int>) {
oneOf(RealmUtil.getField(ModelWithType.FIELD_ACCESS, AccessModel.FIELD_ROLES, IntObject.FIELD_VALUE), roles)
}
return query.build {
group {
isEmpty(ModelWithType.FIELD_ACCESS)
or
group {
isNotEmpty(ModelWithType.FIELD_ACCESS)
and
group {
group {
operationTypeIs(AccessModel.OPERATION_HIDE)
and
not {
hasAnyOfRoles(roles)
}
}
or
group {
not {
operationTypeIs(AccessModel.OPERATION_HIDE)
}
and
hasAnyOfRoles(roles)
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment