f526d4152aa3e4e2a76e19d866604573fc96b737
[chaz/chatty] / lib / Chatty / Controller / Root.pm
1 package Chatty::Controller::Root;
2 use Moose;
3 use namespace::autoclean;
4
5 BEGIN { extends 'Catalyst::Controller' }
6
7 #
8 # Sets the actions in this controller to be registered with no prefix
9 # so they function identically to actions created in MyApp.pm
10 #
11 __PACKAGE__->config(namespace => '');
12
13 use JSON 'encode_json';
14
15 use Chatty::Form::Login;
16 use Chatty::Form::Register;
17
18 has 'login_form' => (
19 isa => 'Chatty::Form::Login',
20 is => 'rw',
21 lazy => 1,
22 default => sub { Chatty::Form::Login->new }
23 );
24
25 has 'register_form' => (
26 isa => 'Chatty::Form::Register',
27 is => 'rw',
28 lazy => 1,
29 default => sub { Chatty::Form::Register->new }
30 );
31
32 =head1 NAME
33
34 Chatty::Controller::Root - Root Controller for Chatty
35
36 =head1 DESCRIPTION
37
38 Implements all actions for this simple chat application.
39
40 =head1 METHODS
41
42 =head2 index
43
44 The root page (/)
45
46 =cut
47
48 sub index :Path :Args(0) {
49 my ($self, $c) = @_;
50 $c->go('/chat/list') if ($c->user_exists);
51 $c->go('login');
52 }
53
54 =head2 login
55
56 Allow a user to login.
57
58 =cut
59
60 sub login :Local :Args(0) {
61 my ($self, $c) = @_;
62
63 $c->stash(form => $self->login_form);
64 $self->login_form->process($c->req->params);
65 return unless $self->login_form->is_valid;
66
67 eval {
68 if ($c->authenticate({
69 username => $self->login_form->value->{username},
70 password => $self->login_form->value->{password}
71 })) {
72 $c->change_session_id;
73 my $user = $c->user->get('username');
74 $c->flash->{message} .= "Hi, $user! You are now logged in.";
75 $c->res->redirect($c->uri_for_action('index'));
76 return;
77 }
78 };
79 $c->flash->{error} = "Log-in failed! Try again, I guess.";
80 $c->res->redirect($c->uri_for_action('login'));
81 }
82
83 =head2 logout
84
85 Log the user out.
86
87 =cut
88
89 sub logout :Local :Args(0) {
90 my ($self, $c) = @_;
91 if ($c->user_exists) {
92 $c->logout;
93 $c->flash->{message} = "Goodbye! You have been logged out.";
94 }
95 $c->res->redirect($c->uri_for_action('index'));
96 }
97
98 =head2 register
99
100 Register a new account.
101
102 =cut
103
104 sub register :Local :Args(0) {
105 my ($self, $c) = @_;
106
107 $c->stash(form => $self->register_form);
108
109 my $new_account = $c->model('DB::Account')->new_result({});
110 $self->register_form->process(
111 item => $new_account,
112 params => $c->req->params
113 );
114
115 if (!$self->register_form->is_valid) {
116 if ($c->req->method eq 'POST') {
117 $c->stash->{error} = "The form has a validation error. Try again...";
118 }
119 return;
120 }
121
122 $c->flash->{message} = "Registration complete. ";
123 $c->forward('login');
124 }
125
126 =head2 register_validate
127
128 Check whether or not a username is available.
129
130 =cut
131
132 sub register_validate :Local :Args(0) {
133 my ($self, $c) = @_;
134
135 my $id = $c->req->param('fieldId');
136 my $username = $c->req->param('fieldValue');
137
138 my $json_arr = [];
139
140 if ($username) {
141 my $account = $c->model('DB::Account')->find({username => $username});
142 if (!$account) {
143 $json_arr = ["$id", 1, "This username is available. Nice!"];
144 }
145 else {
146 $json_arr = ["$id", 0, "This username is taken."];
147 }
148 }
149 else {
150 $json_arr = ["$id", 0, "Invalid arguments to check script."];
151 }
152 $c->res->content_type("application/json");
153 $c->res->body(encode_json($json_arr));
154 }
155
156 =head2 access_denied
157
158 Standard 403 error page
159
160 =cut
161
162 sub access_denied :Private {
163 my ($self, $c) = @_;
164 $c->res->body('Access denied.');
165 $c->res->status(403);
166 }
167
168 =head2 missing
169
170 Standard 404 error page
171
172 =cut
173
174 sub missing :Private {
175 my ($self, $c) = @_;
176 $c->res->body('Page not found.');
177 $c->res->status(404);
178 }
179
180 =head2 default
181
182 Standard 404 error page
183
184 =cut
185
186 sub default :Path {
187 my ($self, $c) = @_;
188 $c->detach('missing');
189 }
190
191 =head2 end
192
193 Attempt to render a view, if needed.
194
195 =cut
196
197 sub end : ActionClass('RenderView') {}
198
199 =head1 AUTHOR
200
201 Charles McGarvey
202
203 =head1 LICENSE
204
205 This library is free software. You can redistribute it and/or modify
206 it under the same terms as Perl itself.
207
208 =cut
209
210 __PACKAGE__->meta->make_immutable;
211
212 1;
This page took 0.050274 seconds and 4 git commands to generate.