Reverted accidental checkin
[castle.git] / Samples / MindDump / Castle.Applications.MindDump / Presentation / Controllers / AccountController.cs
blobc7b7a9bf0b2ec648e7fafa858eec8f6c637dff82
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Applications.MindDump.Presentation.Controllers
17 using System;
18 using System.Collections;
20 using Castle.MonoRail.Framework;
22 using Castle.Applications.MindDump.Model;
23 using Castle.Applications.MindDump.Services;
26 [Layout("default")]
27 public class AccountController : AbstractSecureController
29 private AccountService _accountService;
30 private AuthenticationService _authenticationService;
31 private EncryptionService _encryptionService;
33 public AccountController(AccountService accountService,
34 AuthenticationService authenticationService,
35 EncryptionService encryptionService)
37 _accountService = accountService;
38 _authenticationService = authenticationService;
39 _encryptionService = encryptionService;
42 [SkipFilter]
43 public void New()
47 [SkipFilter]
48 [Rescue("errorcreatingaccount")]
49 public void CreateAccount(String login, String name, String email,
50 String pwd, String pwd2, String blogname,
51 String blogdesc, String theme)
53 // Perform some simple validation
54 if (!IsValid(login, name, email, pwd, pwd2, blogname, blogdesc, theme))
56 RenderView("new");
57 return;
60 Author author = new Author(name, login, pwd);
61 Blog blog = new Blog(blogname, blogdesc, theme, author);
63 _accountService.CreateAccountAndBlog( blog );
65 // Done, now lets log on into the system
66 PerformLogin(login, pwd);
69 [SkipFilter]
70 public void Authentication()
74 [SkipFilter]
75 public void PerformLogin(String login, String pwd)
77 if (!_authenticationService.Authenticate(login, pwd))
79 Context.Flash["errormessage"] = "User not found or incorrect password.";
81 RenderView("Authentication");
83 else
85 DateTime twoWeeks = DateTime.Now.Add( new TimeSpan(14,0,0,0) );
87 Context.Response.CreateCookie("authenticationticket",
88 _encryptionService.Encrypt(login), twoWeeks );
90 Redirect("Maintenance", "newentry");
95 // Private operations to handle common tasks
98 private bool IsValid(string login, string name, string email, string pwd, string pwd2, string blogname, string blogdesc, string theme)
100 ArrayList errors = new ArrayList();
102 if (login.Trim().Length == 0)
104 errors.Add("You must supply a valid login name");
106 if (name.Trim().Length == 0)
108 errors.Add("You must supply a valid name");
110 if (pwd.Trim().Length == 0)
112 errors.Add("You must supply a valid password");
114 if (pwd2.Trim().Length == 0)
116 errors.Add("You must supply a valid password confirmation");
118 else if (!pwd.Equals(pwd2))
120 errors.Add("Passwords don't match...");
122 if (blogname.Trim().Length == 0)
124 errors.Add("You must supply a valid blog name");
127 if (errors.Count == 0) return true;
129 Context.Flash["errormessages"] = errors;
131 return false;