Newer Older
144 lines | 3.944kb
add user update page
Yuki Kimoto authored on 2016-04-07
1
<%
2
  my $api = gitprep_api;
3
  
4
  my $op = param('op') || '';
5
  my $user_id = param('id');
6
  
7
  my $errors;
add validation to user/updat...
Yuki Kimoto authored on 2016-05-04
8
  if (lc $self->req->method eq 'post') {
9
    if ($op eq 'update') {
add user update page
Yuki Kimoto authored on 2016-04-07
10
    
add validation to user/updat...
Yuki Kimoto authored on 2016-05-04
11
      # Parameters
12
      my $id = param('id');
13
      my $name = param('name');
14
      my $email = param('email');
15
      
16
      # Validator
17
      my $vc = app->vc;
add user update page
Yuki Kimoto authored on 2016-04-07
18
      
add validation to user/updat...
Yuki Kimoto authored on 2016-05-04
19
      # Validation result
20
      my $validation = $vc->validation;
21
      
22
      # "id" check
23
      if (!(defined $id && length $id)) {
24
        $validation->add_failed(id => 'User id is empty.');
25
      }
26
      elsif (length $name > 300) {
27
        $validation->add_failed(id => 'User id is too long.');
add user update page
Yuki Kimoto authored on 2016-04-07
28
      }
29
      
add validation to user/updat...
Yuki Kimoto authored on 2016-05-04
30
      # "name" check
31
      $name //= '';
add user update page
Yuki Kimoto authored on 2016-04-07
32
      
add validation to user/updat...
Yuki Kimoto authored on 2016-05-04
33
      # "email" check
34
      if (!(defined $email && length $email)) {
35
        $validation->add_failed(email => "Mail must be not empty");
36
      }
37
      elsif (length $email > 300) {
38
        $validation->add_failed(email => "Mail is too long");
39
      }
40
      elsif ($email !~ /\@/) {
41
        $validation->add_failed(email => "Invalid mail address");
add user update page
Yuki Kimoto authored on 2016-04-07
42
      }
43
      else {
add validation to user/updat...
Yuki Kimoto authored on 2016-05-04
44
        my $where = app->dbi->where;
45
        my $clause = [
46
          'and',
47
          ':email{=}',
48
          ':id{<>}'
49
        ];
50
        my $param = {
51
          email => $email,
52
          id => $user_id
53
        };
54
        $where->clause($clause);
55
        $where->param($param);
56
        
57
        my $row = app->dbi->model('user')->select(where => $where)->one;
58
        if ($row) {
59
          $validation->add_failed(email => "Mail $email already exists");
60
        }
61
      }
62
      
63
      if ($validation->is_valid) {
64
        
65
        # Encrypt password
66
        my $params = {};
67
        $params->{name} = $name;
68
        $params->{email} = $email;
69
        
70
        # Update user
71
        eval { app->dbi->model('user')->update($params, where => {id => $id}) };
72
        if (my $e = $@) {
73
          app->log->error(url_for . ": $e");
74
          $errors = ['Internal Error'];
75
        }
76
        else {
77
          $self->flash(success => 1);
78
          $self->flash(id => $id);
79
          $self->redirect_to(url_for->query(id => $id));
80
        }
add user update page
Yuki Kimoto authored on 2016-04-07
81
      }
add validation to user/updat...
Yuki Kimoto authored on 2016-05-04
82
      else { $errors = $validation->messages }
add user update page
Yuki Kimoto authored on 2016-04-07
83
    }
84
  }
85
  
86
  my $user = app->dbi->model('user')->select(
87
    where => {id => $user_id, admin => 0}
88
  )->one;
89
%>
90

            
91
% layout 'common', title => 'Update User';
92

            
93
  %= include '/include/header';
94

            
95
  <div class="container">
96
    % my $id = '';
97
    % if (flash('success')) {
98
      <div class="alert alert-success">
99
        <button type="button" class="close" data-dismiss="alert">&times;</button>
100
        Success: User <b><%= flash('id') %></b> is updated.
101
      </div>
102
    % }
103
    
104
    % if ($errors) {
105
      <div class="alert alert-error">
106
        <button type="button" class="close" data-dismiss="alert">&times;</button>
107
        % for my $error (@$errors) {
108
          <p><%= $error %></p>
109
        % }
110
      </div>
111
    % }
112
    
113
    <div class="topic1" style="text-align:center">Update User</div>
114
    <form class="user-form" action="<%= url_for->query(id => $user_id) %>" method="post">
115
      %= hidden_field op => 'update';
116
      <div class="user-form-container">
117
        <div>
118
          <div><b>ID</b></div>
119
          <div>
120
            <%= $user->{id} %>
121
          </div>
122
        </div>
123
        <div>
124
          <div><b>Name</b></div>
125
          <div>
126
            <%= text_field 'name' => $user->{name}, placeholder => 'Name' %>
127
          </div>
128
        </div>
129
        <div>
130
          <div><b>Mail</b></div>
131
          <div>
fix user create and update b...
Yuki Kimoto authored on 2016-04-21
132
            <%= text_field 'email' => $user->{email}, placeholder => 'Mail' %>
add user update page
Yuki Kimoto authored on 2016-04-07
133
          </div>
134
        </div>
135
        <div>
136
          <div>
rename btn-success to btn-gr...
Yuki Kimoto authored on 2016-11-30
137
            <button type="submit" class="btn btn-green" style="margin-top:20px">Update User</button>
add user update page
Yuki Kimoto authored on 2016-04-07
138
          </div>
139
        </div>
140
      </div>
141
    </form>
142
    <div style="text-align:center;margin:20px 0"><big><a href="<%= url_for('/_admin/users') %>">Users</a></big></div>
143
  </div>
144
  %= include '/include/footer';