Mercurial > altnet-hispano
annotate Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorController.cs @ 292:1408ac17cb64
Patrocinadores: Faltaba aplicar seguridad a la Api de Patrocinadores.
author | juanjose.montesdeocaarbos |
---|---|
date | Thu, 09 Feb 2012 08:40:38 -0300 |
parents | 1e889a2e45c5 |
children | 8c742d5ccf67 |
rev | line source |
---|---|
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
1 using System; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
2 using System.Collections.Generic; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
3 using System.IO; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
4 using System.Linq; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
5 using System.Web; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
6 using System.Web.Mvc; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
7 using AltNetHispano.Agendas.Domain; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
8 using AltNetHispano.Agendas.Factories; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
9 using AltNetHispano.Agendas.Web.Models; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
10 using AltNetHispano.Agendas.Web.Services; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
11 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
12 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
13 namespace AltNetHispano.Agendas.Web.Controllers |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
14 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
15 public class PatrocinadorController : Controller |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
16 { |
278 | 17 private HttpServerUtilityBase _server; |
18 | |
19 public HttpServerUtilityBase HttpServer | |
20 { | |
21 get | |
22 { | |
23 if (_server == null) | |
24 { | |
25 var httpServer = HttpContext.Server; | |
26 _server = httpServer; | |
27 } | |
28 | |
29 return _server; | |
30 } | |
31 } | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
32 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
33 public PatrocinadorController() |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
34 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
35 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
36 |
278 | 37 public PatrocinadorController(HttpServerUtilityBase server) |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
38 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
39 _server = server; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
40 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
41 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
42 public ActionResult GetLogo(string id) |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
43 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
44 var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
45 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
46 var patrocinador = patrocinadores.Get(new Guid(id)); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
47 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
48 return File(patrocinador.Logo, "image/jpg"); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
49 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
50 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
51 public ActionResult Index() |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
52 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
53 var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
54 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
55 var model = new PatrocinadorIndexModel |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
56 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
57 Items = from p in patrocinadores.GetAll() |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
58 select new PatrocinadorDto |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
59 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
60 Id = p.Id, |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
61 Nombre = p.Nombre |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
62 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
63 }; |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
64 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
65 return View(model); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
66 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
67 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
68 [CustomAuthorize(Roles = Roles.Administrador)] |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
69 public ActionResult Nuevo() |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
70 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
71 var model = new PatrocinadorNewModel(); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
72 return View("DefaultEditor", model); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
73 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
74 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
75 [HttpPost] |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
76 [CustomAuthorize(Roles = Roles.Administrador)] |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
77 public ActionResult Nuevo(PatrocinadorNewModel model) |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
78 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
79 if (ModelState.IsValid) |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
80 { |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
81 var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
82 |
278 | 83 if (patrocinadores.GetByNombre(model.Nombre) == null) |
84 { | |
279 | 85 var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"), |
86 model.Nombre + | |
87 model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf("."))); | |
88 model.Logo.SaveAs(tempLogoFile); | |
278 | 89 var patrocinador = new Patrocinador(model.Nombre); |
90 patrocinador.LoadLogo(tempLogoFile); | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
91 |
278 | 92 patrocinadores.Save(patrocinador); |
93 | |
94 return RedirectToAction("Index"); | |
95 } | |
96 this.AddError("El patrocinador ya existe."); | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
97 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
98 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
99 return View("DefaultEditor", model); |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
100 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
101 |
278 | 102 [CustomAuthorize(Roles = Roles.Administrador)] |
103 public ActionResult Modificar(string id) | |
104 { | |
105 var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
106 |
278 | 107 var patrocinador = patrocinadores.GetById(id); |
108 if (patrocinador == null) | |
109 { | |
110 this.AddError("No se encontró el patrocinador que intenta modificar"); | |
111 return RedirectToAction("Index"); | |
112 } | |
279 | 113 var model = new PatrocinadorEditModel |
114 { | |
115 Id = new Guid(id).ToString(), | |
116 Nombre = patrocinador.Nombre | |
117 }; | |
278 | 118 return View("DefaultEditor", model); |
119 } | |
120 | |
121 [HttpPost] | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
122 [CustomAuthorize(Roles = Roles.Administrador)] |
278 | 123 public ActionResult Modificar(PatrocinadorEditModel model) |
124 { | |
125 if (ModelState.IsValid) | |
126 { | |
127 var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); | |
279 | 128 var patrocinador = patrocinadores.GetById(model.Id); |
129 if (patrocinador != null) | |
278 | 130 { |
279 | 131 var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"), |
132 model.Nombre + | |
133 model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf("."))); | |
134 model.Logo.SaveAs(tempLogoFile); | |
135 patrocinador.Nombre = model.Nombre; | |
278 | 136 patrocinador.LoadLogo(tempLogoFile); |
137 | |
138 patrocinadores.Save(patrocinador); | |
139 | |
140 return RedirectToAction("Index"); | |
141 } | |
142 this.AddError("No se encuentra el patrocinador."); | |
143 } | |
144 return View("DefaultEditor", model); | |
145 } | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
146 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
147 [CustomAuthorize(Roles = Roles.Administrador)] |
279 | 148 public ActionResult Eliminar(string id) |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
149 { |
279 | 150 if (!string.IsNullOrWhiteSpace(id)) |
151 { | |
152 var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); | |
153 var patrocinador = patrocinadores.GetById(id); | |
154 | |
155 patrocinadores.Delete(patrocinador); | |
156 } | |
157 return RedirectToAction("Index"); | |
158 } | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
159 |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
160 } |
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
161 } |