canvas-lms/lib/api/v1/group.rb

64 lines
2.4 KiB
Ruby
Raw Normal View History

2011-02-01 09:57:29 +08:00
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
module Api::V1::Group
include Api::V1::Json
include Api::V1::Context
2011-02-01 09:57:29 +08:00
API_GROUP_JSON_OPTS = {
:only => %w(id name description is_public join_level group_category_id),
:methods => %w(members_count storage_quota_mb),
}
API_GROUP_MEMBERSHIP_JSON_OPTS = {
:only => %w(id group_id user_id workflow_state moderator)
}
Add a permission option in the api to return if the user can create topics. closes CNVS-6824 This adds a permissions attribute to the returned json for discussion topic contexts (Course, Group). The permissions attribute contains an optional permission of "create_discission_topic" which returns true or false depending on whether the current user can create discussion topics for the course or group. For performance reasons this is only added to a single course/group json and not in lists so the only call that will return it is /api/v1/<context>/<context_id> where context is a course or group. Since we did not want to include this on every response its a custom permissions attribute for course and groups in the course_json or group_json serialization methods. Using the includes parameter for the API supplying a value of "permissions" will include the permissions with "create_discussion_topic" for a group and course. When the object is serialized to json it checked to see if the model implements a serialize_permissions method and calls that to render or override permissions generated from the policies. - Create a test Course. Make sure the course allows members to post topics. - Add a student to the course. - Make a call to /api/v1/courses/<id> where "<id>" is the id of the created course. - The response should include a permissions attribute with a boolean value for "create_discission_topic" see the example below. - Make a call to /api/v1/courses to return a list of course objects. - The permissions attribute should not be included in the response. - Create a test Group tied to the course created in the first step. - Add a mamber to the group. - Make a call to /api/v1/groups/<id> where "<id>" is the id of the created group. - The response should include a permissions attribute with a boolean value for "create_discission_topic" see the example below. - Make a call to /api/v1/groups to return a list of group objects. - The permissions attribute should not be included in the response. Example Response: { id: 42, ... permissions: { create_discission_topic: true } } Change-Id: Ia02d5aa67e345740a93dd0f63e357e7cb5e1efd6 Reviewed-on: https://gerrit.instructure.com/24478 Reviewed-by: Jacob Fugal <jacob@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: August Thornton <august@instructure.com> Product-Review: Nick Cloward <ncloward@instructure.com>
2013-09-18 06:24:57 +08:00
API_PERMISSIONS_TO_INCLUDE = %w(create_discussion_topic)
def group_json(group, user, session, options = {})
Add a permission option in the api to return if the user can create topics. closes CNVS-6824 This adds a permissions attribute to the returned json for discussion topic contexts (Course, Group). The permissions attribute contains an optional permission of "create_discission_topic" which returns true or false depending on whether the current user can create discussion topics for the course or group. For performance reasons this is only added to a single course/group json and not in lists so the only call that will return it is /api/v1/<context>/<context_id> where context is a course or group. Since we did not want to include this on every response its a custom permissions attribute for course and groups in the course_json or group_json serialization methods. Using the includes parameter for the API supplying a value of "permissions" will include the permissions with "create_discussion_topic" for a group and course. When the object is serialized to json it checked to see if the model implements a serialize_permissions method and calls that to render or override permissions generated from the policies. - Create a test Course. Make sure the course allows members to post topics. - Add a student to the course. - Make a call to /api/v1/courses/<id> where "<id>" is the id of the created course. - The response should include a permissions attribute with a boolean value for "create_discission_topic" see the example below. - Make a call to /api/v1/courses to return a list of course objects. - The permissions attribute should not be included in the response. - Create a test Group tied to the course created in the first step. - Add a mamber to the group. - Make a call to /api/v1/groups/<id> where "<id>" is the id of the created group. - The response should include a permissions attribute with a boolean value for "create_discission_topic" see the example below. - Make a call to /api/v1/groups to return a list of group objects. - The permissions attribute should not be included in the response. Example Response: { id: 42, ... permissions: { create_discission_topic: true } } Change-Id: Ia02d5aa67e345740a93dd0f63e357e7cb5e1efd6 Reviewed-on: https://gerrit.instructure.com/24478 Reviewed-by: Jacob Fugal <jacob@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: August Thornton <august@instructure.com> Product-Review: Nick Cloward <ncloward@instructure.com>
2013-09-18 06:24:57 +08:00
includes = options[:include] || []
permissions_to_include = API_PERMISSIONS_TO_INCLUDE if includes.include?('permissions')
hash = api_json(group, user, session, API_GROUP_JSON_OPTS, permissions_to_include)
hash.merge!(context_data(group))
image = group.avatar_attachment
hash['avatar_url'] = image && thumbnail_image_url(image, image.uuid)
hash['role'] = group.group_category.role if group.group_category
Add a permission option in the api to return if the user can create topics. closes CNVS-6824 This adds a permissions attribute to the returned json for discussion topic contexts (Course, Group). The permissions attribute contains an optional permission of "create_discission_topic" which returns true or false depending on whether the current user can create discussion topics for the course or group. For performance reasons this is only added to a single course/group json and not in lists so the only call that will return it is /api/v1/<context>/<context_id> where context is a course or group. Since we did not want to include this on every response its a custom permissions attribute for course and groups in the course_json or group_json serialization methods. Using the includes parameter for the API supplying a value of "permissions" will include the permissions with "create_discussion_topic" for a group and course. When the object is serialized to json it checked to see if the model implements a serialize_permissions method and calls that to render or override permissions generated from the policies. - Create a test Course. Make sure the course allows members to post topics. - Add a student to the course. - Make a call to /api/v1/courses/<id> where "<id>" is the id of the created course. - The response should include a permissions attribute with a boolean value for "create_discission_topic" see the example below. - Make a call to /api/v1/courses to return a list of course objects. - The permissions attribute should not be included in the response. - Create a test Group tied to the course created in the first step. - Add a mamber to the group. - Make a call to /api/v1/groups/<id> where "<id>" is the id of the created group. - The response should include a permissions attribute with a boolean value for "create_discission_topic" see the example below. - Make a call to /api/v1/groups to return a list of group objects. - The permissions attribute should not be included in the response. Example Response: { id: 42, ... permissions: { create_discission_topic: true } } Change-Id: Ia02d5aa67e345740a93dd0f63e357e7cb5e1efd6 Reviewed-on: https://gerrit.instructure.com/24478 Reviewed-by: Jacob Fugal <jacob@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: August Thornton <august@instructure.com> Product-Review: Nick Cloward <ncloward@instructure.com>
2013-09-18 06:24:57 +08:00
if includes.include?('users')
# TODO: this should be switched to user_display_json
hash['users'] = group.users.map{ |u| user_json(u, user, session) }
end
Add a permission option in the api to return if the user can create topics. closes CNVS-6824 This adds a permissions attribute to the returned json for discussion topic contexts (Course, Group). The permissions attribute contains an optional permission of "create_discission_topic" which returns true or false depending on whether the current user can create discussion topics for the course or group. For performance reasons this is only added to a single course/group json and not in lists so the only call that will return it is /api/v1/<context>/<context_id> where context is a course or group. Since we did not want to include this on every response its a custom permissions attribute for course and groups in the course_json or group_json serialization methods. Using the includes parameter for the API supplying a value of "permissions" will include the permissions with "create_discussion_topic" for a group and course. When the object is serialized to json it checked to see if the model implements a serialize_permissions method and calls that to render or override permissions generated from the policies. - Create a test Course. Make sure the course allows members to post topics. - Add a student to the course. - Make a call to /api/v1/courses/<id> where "<id>" is the id of the created course. - The response should include a permissions attribute with a boolean value for "create_discission_topic" see the example below. - Make a call to /api/v1/courses to return a list of course objects. - The permissions attribute should not be included in the response. - Create a test Group tied to the course created in the first step. - Add a mamber to the group. - Make a call to /api/v1/groups/<id> where "<id>" is the id of the created group. - The response should include a permissions attribute with a boolean value for "create_discission_topic" see the example below. - Make a call to /api/v1/groups to return a list of group objects. - The permissions attribute should not be included in the response. Example Response: { id: 42, ... permissions: { create_discission_topic: true } } Change-Id: Ia02d5aa67e345740a93dd0f63e357e7cb5e1efd6 Reviewed-on: https://gerrit.instructure.com/24478 Reviewed-by: Jacob Fugal <jacob@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: August Thornton <august@instructure.com> Product-Review: Nick Cloward <ncloward@instructure.com>
2013-09-18 06:24:57 +08:00
hash['html_url'] = group_url(group) if includes.include? 'html_url'
hash['sis_group_id'] = group.sis_source_id if group.context_type == 'Account' && group.root_account.grants_rights?(user, session, :read_sis, :manage_sis).values.any?
hash['sis_import_id'] = group.sis_batch_id if group.context_type == 'Account' && group.root_account.grants_right?(user, session, :manage_sis)
hash
2011-02-01 09:57:29 +08:00
end
def group_membership_json(membership, user, session, options = {})
includes = options[:include] || []
hash = api_json(membership, user, session, API_GROUP_MEMBERSHIP_JSON_OPTS)
if includes.include?('just_created')
hash['just_created'] = membership.just_created || false
end
hash
end
2011-02-01 09:57:29 +08:00
end