You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
1.9 KiB

require "uuid"
require "uuid/json"
class TodoD
end
class TodoD::List
alias PermissionLevel = ::AuthD::User::PermissionLevel
JSON.mapping({
id: String,
title: String,
permissions: Hash(String, Array(Int32)),
extra_properties: Hash(String, JSON::Any),
tasks: {
type: Array(String),
default: [] of String
}
})
def initialize(@title, user, @extra_properties = Hash(String, JSON::Any).new)
@id = UUID.random.to_s
@permissions = {
"admin" => [user.uid],
"edit" => [] of Int32,
"read" => [] of Int32
}
@tasks = [] of String
end
def users_with_read_permissions : Array(Int32)
@permissions.map do |level, uids|
# Would raise on invalid level.
level = PermissionLevel.parse level
uids
end.flatten
end
def is_admin?(uid)
@permissions["admin"].any? &.==(uid)
end
def is_editor?(uid)
@permissions.any? do |level, uids|
level = PermissionLevel.parse level
uids.any? do |id|
id == uid && level >= PermissionLevel::Edit
end
end
end
def is_reader?(uid)
@permissions.any? do |level, uids|
level = PermissionLevel.parse level
uids.any? do |id|
id == uid && level >= PermissionLevel::Read
end
end
end
def set_permission(uid : Int32, level : PermissionLevel)
@permissions.each &.[1].select!(&.!=(uid))
return if level.none?
@permissions[level.to_s].push uid
end
end
class TodoD::Task
# Should we hardcode more properties here?
JSON.mapping({
id: String,
list: String,
title: String,
description: {
type: String,
default: ""
},
creator: Int32,
assigned_to: Int32?,
priority: String?,
tags: {
type: Array(String),
default: [] of String
},
extra_properties: {
type: Hash(String, JSON::Any),
default: {} of String => JSON::Any
}
})
def initialize(@list, @title, @creator, @description = "", @assigned_to = nil, @priority = nil, @tags = [] of String, @extra_properties = {} of String => JSON::Any)
@id = UUID.random.to_s
end
end