fix some inaccuracies in the documentation
[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 Chatty::Form::Login;
14 use Chatty::Form::Register;
15
16 has 'login_form' => (
17 isa => 'Chatty::Form::Login',
18 is => 'rw',
19 lazy => 1,
20 default => sub { Chatty::Form::Login->new }
21 );
22
23 has 'register_form' => (
24 isa => 'Chatty::Form::Register',
25 is => 'rw',
26 lazy => 1,
27 default => sub { Chatty::Form::Register->new }
28 );
29
30 =head1 NAME
31
32 Chatty::Controller::Root - Root Controller for Chatty
33
34 =head1 DESCRIPTION
35
36 Implements all actions for this simple chat application.
37
38 =head1 METHODS
39
40 =head2 index
41
42 The root page (/)
43
44 =cut
45
46 sub index :Path :Args(0) {
47 my ($self, $c) = @_;
48 $c->go('/chat/list') if ($c->user_exists);
49 $c->go('login');
50 }
51
52 =head2 login
53
54 Allow a user to login.
55
56 =cut
57
58 sub login :Local :Args(0) {
59 my ($self, $c) = @_;
60
61 $c->stash(form => $self->login_form);
62 $self->login_form->process($c->req->params);
63 return unless $self->login_form->is_valid;
64
65 eval {
66 if ($c->authenticate({
67 username => $self->login_form->value->{username},
68 password => $self->login_form->value->{password}
69 })) {
70 $c->change_session_id;
71 my $user = $c->user->get('username');
72 $c->flash->{message} .= "Hi, $user! You are now logged in.";
73 $c->res->redirect($c->uri_for_action('index'));
74 return;
75 }
76 else {
77 $c->flash->{error} = "Log-in failed! Try again, I guess.";
78 $c->res->redirect($c->uri_for_action('login'));
79 }
80 };
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 if ($username) {
139 my $account = $c->model('DB::Account')->find({username => $username});
140 if (!$account) {
141 $c->stash->{json} = ["$id", 1, "This username is available. Nice!"];
142 }
143 else {
144 $c->stash->{json} = ["$id", 0, "This username is taken."];
145 }
146 }
147 else {
148 $c->stash->{json} = ["$id", 0, "Invalid arguments to check script."];
149 }
150 $c->forward('View::JSON');
151 }
152
153 =head2 access_denied
154
155 Standard 403 error page
156
157 =cut
158
159 sub access_denied :Private {
160 my ($self, $c) = @_;
161 $c->res->body('Access denied.');
162 $c->res->status(403);
163 }
164
165 =head2 missing
166
167 Standard 404 error page
168
169 =cut
170
171 sub missing :Private {
172 my ($self, $c) = @_;
173 $c->res->body('Page not found.');
174 $c->res->status(404);
175 }
176
177 =head2 default
178
179 Standard 404 error page
180
181 =cut
182
183 sub default :Path {
184 my ($self, $c) = @_;
185 $c->detach('missing');
186 }
187
188 =head2 end
189
190 Attempt to render a view, if needed.
191
192 =cut
193
194 sub end : ActionClass('RenderView') {}
195
196 =head1 AUTHOR
197
198 Charles McGarvey
199
200 =head1 LICENSE
201
202 This library is free software. You can redistribute it and/or modify
203 it under the same terms as Perl itself.
204
205 =cut
206
207 __PACKAGE__->meta->make_immutable;
208
209 1;
This page took 0.041718 seconds and 4 git commands to generate.