// // CircleController.cs // // Author: // Paul Schneider // // Copyright (c) 2015 GNU GPL // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . using System; using System.Web.Http; using Yavsc.Model.RolesAndMembers; using System.Collections.Generic; using Yavsc.Model.Circles; using System.Web.Security; using System.Collections.Specialized; using Yavsc.Model; namespace Yavsc.ApiControllers { /// /// Circle controller. /// public class CircleController : ApiController { /// /// Create the specified circle. /// /// Model. [Authorize, AcceptVerbs ("POST")] public long Create(Circle model) { string user = Membership.GetUser ().UserName; return CircleManager.DefaultProvider.Create (user, model.Title, model.Members); } /// /// Add the specified users to the circle. /// /// Circle Identifier. /// username. [Authorize, AcceptVerbs ("POST")] public void AddUserToCircle(long id, string username) { checkIsOwner (CircleManager.DefaultProvider.Get (id)); CircleManager.DefaultProvider.AddMember (id, username); } /// /// Delete the circle specified by id. /// /// Identifier. [Authorize, AcceptVerbs ("GET")] public void Delete(long id) { checkIsOwner (CircleManager.DefaultProvider.Get (id)); CircleManager.DefaultProvider.Delete (id); } /// /// Removes the user from circle. /// /// Identifier. /// Username. [Authorize, AcceptVerbs ("GET")] public void RemoveUserFromCircle(long id, string username) { checkIsOwner (CircleManager.DefaultProvider.Get(id)); CircleManager.DefaultProvider.RemoveMembership (id,username); } private void checkIsOwner(CircleBase c) { string user = Membership.GetUser ().UserName; if (c.Owner != user) throw new AccessViolationException ("You're not owner of this circle"); } /// /// Get the circle specified id. /// /// Identifier. [Authorize, AcceptVerbs ("GET")] public Circle Get(long id) { var c = CircleManager.DefaultProvider.GetMembers (id); checkIsOwner (c); return c; } /// /// List the circles /// [Authorize, AcceptVerbs ("GET")] public IEnumerable List() { string user = Membership.GetUser ().UserName; return CircleManager.DefaultProvider.List (user); } /// /// List the circles /// [Authorize, AcceptVerbs ("POST")] public void Update(CircleBase circle) { string user = Membership.GetUser ().UserName; CircleBase current = CircleManager.DefaultProvider.Get (circle.Id); if (current.Owner != user) throw new AuthorizationDenied ("Your not owner of circle at id "+circle.Id); CircleManager.DefaultProvider.UpdateCircle (circle); } } }