# /tcl/km-access-procs.tcl # # Functions related to access control. # # Created by carsten@arsdigita.com in June 2000 # # $Id: km-access-procs.tcl,v 1.2 2009/02/13 20:40:39 jeffd Exp $ ad_proc km_check_object_id { { -check_deleted_p 1 -check_read_p 1 -check_edit_p 0 -check_delete_p 0 -print_errors_p 1 } object_id } { Check if the object exists, and print an error message if it doesn't. Returns 0 if the object_id is invalid. If check_deleted_p is 1, also checks if the object has been deleted. If check_read_p is 1, also checks if the user may view the object. If check_edit_p is 1, also checks if the user may edit the object. Only complains to the user if print_errors_p is 1. } { set package_id [ad_conn package_id] set user_id [ad_conn user_id] if { ![db_0or1row get_object_info " select decode(sign(sysdate-o.expiration_date),1,1,0,1,0) as deleted_p, o.original_author_id, o.expiration_date, o.object_type_id, o.one_line_description as object_name, o.overview, o.last_modifying_user_id, o.start_date, o.end_date, decode(o.public_p,'t',1,0) as public_p, o.public_until, decode(o.archived_p,'t',1,0) as archived_p, o.original_author_id, to_char(o.creation_date) as creation_date, to_char(o.last_modified) as last_modified, to_char(o.user_checkoff_date) as user_checkoff_date, u.first_names || ' ' || u.last_name as original_author_name, u.email as original_author_email, o.publisher_id, decode([tcl_permission_p ":object_id" ":user_id" "'read'"],'t',1,0) as read_p, decode([tcl_permission_p ":object_id" ":user_id" "'write'"],'t',1,0) as write_p, decode([tcl_permission_p ":object_id" ":user_id" "'delete'"],'t',1,0) as delete_p, decode([tcl_permission_p ":package_id" ":user_id" "'admin'"],'t',1,0) as admin_p, decode([tcl_permission_p ":package_id" ":user_id" "'km_publish'"],'t',1,0) as publish_p, decode(acs_permission.permission_p(o.object_type_id, :user_id, 'create'),'t',1,0) as create_p, nvl(o.access_total,0) as access_total, nvl(o.access_month,0) as access_month, decode(o.in_review_p,'t',1,0) as in_review_p from sn_objects o, users u where o.object_id = :object_id and u.user_id = o.original_author_id "] } { # Error: The object does not exist. if { $print_errors_p } { ad_return_exception_page 404 "Entry does not exist." " The entry with id $object_id does not exist. We never really delete object IDs from the database so this is quite unusual. Unless you tried to hack the URL manually this might be a bug in our software." } return 0 } if { [empty_string_p $object_name] } { set object_name "Unnamed [km_static object_type_pretty_name $object_type_id]" } # review process enabled? if { [km_static approval_p $package_id] && !$publish_p } { if {$public_p || $in_review_p} { # revoke delete permission from object owners if object is # public or review process started set delete_p $admin_p } } # The state of a workflow task is user-dependant! if {$in_review_p} { set state [db_string workflow_case_state {select distinct t.transition_key from wf_cases c, wf_user_tasks t where c.case_id = t.case_id and c.object_id=:object_id and c.state = 'active' and rownum = 1 order by t.transition_key desc} -default ""] } else { set state "" } ## and t.user_id = :user_id km_conn -reset km_conn -set public_p $public_p km_conn -set archived_p $archived_p km_conn -set deleted_p $deleted_p km_conn -set expiration_date $expiration_date km_conn -set object_type_id $object_type_id km_conn -set object_name $object_name km_conn -set overview $overview km_conn -set original_author_id $original_author_id km_conn -set original_author_name $original_author_name km_conn -set original_author_email $original_author_email km_conn -set creation_date $creation_date km_conn -set last_modified $last_modified km_conn -set user_checkoff_date $user_checkoff_date km_conn -set admin_p $admin_p km_conn -set publish_p $publish_p km_conn -set read_p $read_p km_conn -set write_p $write_p km_conn -set delete_p $delete_p km_conn -set create_p $create_p km_conn -set last_modifying_user_id $last_modifying_user_id km_conn -set access_total $access_total km_conn -set access_month $access_month km_conn -set public_until $public_until km_conn -set start_date $start_date km_conn -set end_date $end_date km_conn -set publisher_id $publisher_id km_conn -set in_review_p $in_review_p km_conn -set review_state $state if { $check_deleted_p && $deleted_p } { # Error: The object has been deleted. set expiration_date [util_AnsiDatetoPrettyDate $expiration_date] if {![empty_string_p $last_modifying_user_id]} { # If we know who deleted the object, we want to give the # user a link to that user's community page. set deletor_name [db_string km_check_object_id_2 " select first_names || ' ' || last_name from users where user_id=:last_modifying_user_id"] set deletion_text "This object was deleted on $expiration_date by [ad_present_user $last_modifying_user_id $deletor_name]." set reason_for_deleting [db_string km_check_object_id_3 " select reason_for_deleting from sn_object_delete_reasons where object_id=:object_id" -default ""] if {![empty_string_p $reason_for_deleting]} { append deletion_text "
Reason:
$reason_for_deleting" } } else { set deletion_text "This object was deleted on $expiration_date." } if { $print_errors_p } { ad_returnredirect "object-deleted?object_id=$object_id" } return 0 } if { $check_read_p && !$read_p } { # A user only has read access if the object is public, # or he has been given read permission. if { $print_errors_p } { ad_return_forbidden "Not Authorized" \ "You are not authorized to view this object." } return 0 } elseif { $check_edit_p && !$admin_p && (!$write_p || $archived_p) } { if { $print_errors_p } { ad_return_forbidden "Not Authorized" \ "You are not authorized to edit this object." } return 0 } elseif { $check_delete_p && !$admin_p && !$delete_p } { if { $print_errors_p } { ad_return_forbidden "Not Authorized" \ "You are not authorized to delete this object." } return 0 } # Check if the object type (still) exists, and if the user is allowed # to view it. if { [km_static object_type_deleted_p $object_type_id] } { if { $print_errors_p } { # Only complain to the user if we were told so. set pretty_type [km_static object_type_pretty_name $object_type_id] ad_return_exception_page 404 "The object type \"$pretty_type\" has been removed from the system." "The object type \"$pretty_type\" has been removed from the system. You therefore cannot view this object" } return 0 } if { $check_read_p && ![km_static object_type_public_p $object_type_id] && !$admin_p } { # The object type may not be viewed by this user. if { $print_errors_p } { ad_return_forbidden "Not Authorized" \ "You are not authorized to view this object." } return 0 } return 1 } ad_proc km_check_object_type_id { { -check_view_p 1 -check_create_p 0 -print_errors_p 1 } object_type_id } { Check if the user may access the given object type. Checks for read and create permissions, whether the object type has been deleted, and whether it belongs to the current package. If check_create_p is set, also complains when the object type is not browsable. } { set package_id [ad_conn package_id] set user_id [ad_conn user_id] if { ![db_0or1row km_check_object_type_id " select deleted_p, browse_p, public_p, decode([tcl_permission_p ":package_id" ":user_id" "'admin'"],'t',1,0) as admin_p, decode([tcl_permission_p ":object_type_id" ":user_id" "'create'"],'t',1,0) as create_p from sn_object_types where object_type_id = :object_type_id and context_id = :package_id "] } { if { $print_errors_p } { ad_return_exception_page 404 "Invalid object_type_id" \ "The given object_type_id is invalid." } return 0 } km_conn -reset km_conn -set admin_p $admin_p km_conn -set create_p $create_p if { $deleted_p == "t" } { if { $print_errors_p } { ad_return_exception_page 410 "Object type no longer exists" \ "This object type has been removed from the system." } return 0 } if { $check_view_p && $public_p == "f" && !$admin_p } { if { $print_errors_p } { ad_return_forbidden "Not Authorized" \ "You are not authorized to view objects of this type." } return 0 } if { $check_create_p && ($public_p == "f" || !$create_p) && !$admin_p } { if { $print_errors_p } { ad_return_forbidden "Not Authorized" \ "You are not authorized to create objects of this type." } return 0 } return 1 } ad_proc km_check_owner_change { { -print_errors_p 1 } object_id } { Checks if the user may change the ownership of the given object. This is only allowed for the object owner and administrators. } { set user_id [ad_conn user_id] set package_id [ad_conn package_id] set result [db_string check_owner_change " select decode(count(*),0,0,1) from dual where exists (select 1 from sn_objects where object_id = :object_id and context_id = :package_id and (original_author_id = :user_id or [tcl_permission_p ":package_id" ":user_id" "'admin'"] = 't')) "] if { !$result && $print_errors_p } { ad_return_forbidden "Not Authorized" \ "You are not authorized to change the ownership of this object." } return $result } ad_proc km_object_type_private_p {object_type_id} { Checks if an object type is not visible to the public. } { set context_id [ad_conn package_id] set result [db_string km_object_type_private_p " select decode(public_p,'t',0,1) from sn_object_types where object_type_id=:object_type_id" -default ""] return $result }