« blib/lib/App/Netdisco/Worker/Plugin/DumpConfig.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/IOS.pm »
| 1 | package App::Netdisco::Web::Plugin::AdminTask::Users; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::Ajax; | ||
| 5 | use Dancer::Plugin::DBIC; | ||
| 6 | use Dancer::Plugin::Auth::Extensible; | ||
| 7 | use Dancer::Plugin::Passphrase; | ||
| 8 | |||
| 9 | use App::Netdisco::Web::Plugin; | ||
| 10 | use App::Netdisco::Util::Token 'random_token'; | ||
| 11 | use List::MoreUtils 'uniq'; | ||
| 12 | use Digest::MD5 (); | ||
| 13 | |||
| 14 | register_admin_task({ | ||
| 15 | tag => 'users', | ||
| 16 | label => 'User Management', | ||
| 17 | provides_csv => 1, | ||
| 18 | }); | ||
| 19 | |||
| 20 | sub _sanity_ok { | ||
| 21 | return 0 unless param('username') | ||
| 22 | and param('username') =~ m/^[[:print:] ]+$/; | ||
| 23 | return 1; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub _make_password { | ||
| 27 | my $pass = (shift || passphrase->generate_random); | ||
| 28 | if (setting('safe_password_store')) { | ||
| 29 | return passphrase($pass)->generate; | ||
| 30 | } | ||
| 31 | else { | ||
| 32 | return Digest::MD5::md5_hex($pass), | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _port_control_fields { | ||
| 37 | my $mode = param('port_control') || ''; | ||
| 38 | return (port_control => \'false', portctl_roles => undef) unless $mode; | ||
| 39 | return (port_control => \'true', portctl_roles => undef) if $mode eq '_global_'; | ||
| 40 | |||
| 41 | send_error('Bad Request', 400) unless $mode eq '_roles_'; | ||
| 42 | |||
| 43 | my $given = param('portctl_roles'); | ||
| 44 | my @roles = uniq grep { defined and length } | ||
| 45 | (ref $given eq ref [] ? @$given : ($given)); | ||
| 46 | send_error('Bad Request', 400) unless scalar @roles; | ||
| 47 | |||
| 48 | return (port_control => \'true', portctl_roles => \@roles); | ||
| 49 | } | ||
| 50 | |||
| 51 | ajax '/ajax/control/admin/users/add' => require_role setting('defanged_admin') => sub { | ||
| 52 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 53 | my %port_control = _port_control_fields(); | ||
| 54 | |||
| 55 | schema('netdisco')->txn_do(sub { | ||
| 56 | schema('netdisco')->resultset('User') | ||
| 57 | ->create({ | ||
| 58 | username => param('username'), | ||
| 59 | fullname => param('fullname'), | ||
| 60 | |||
| 61 | (param('auth_method') eq 'permanent_token' ? ( | ||
| 62 | password => undef, | ||
| 63 | ldap => \'false', radius => \'false', tacacs => \'false', | ||
| 64 | token_auth_only => \'true', | ||
| 65 | token_no_expire => \"true", | ||
| 66 | token_acl => (param('token_acl') || undef), | ||
| 67 | ) : ( | ||
| 68 | password => _make_password(param('password')), | ||
| 69 | token_auth_only => \'false', | ||
| 70 | token_no_expire => \"false", | ||
| 71 | token_acl => undef, | ||
| 72 | (param('auth_method') ? ( | ||
| 73 | (ldap => (param('auth_method') eq 'ldap' ? \'true' : \'false')), | ||
| 74 | (radius => (param('auth_method') eq 'radius' ? \'true' : \'false')), | ||
| 75 | (tacacs => (param('auth_method') eq 'tacacs' ? \'true' : \'false')), | ||
| 76 | ) : ( | ||
| 77 | ldap => \'false', | ||
| 78 | radius => \'false', | ||
| 79 | tacacs => \'false', | ||
| 80 | )), | ||
| 81 | )), | ||
| 82 | |||
| 83 | %port_control, | ||
| 84 | |||
| 85 | admin => (param('admin') ? \'true' : \'false'), | ||
| 86 | note => param('note'), | ||
| 87 | }); | ||
| 88 | }); | ||
| 89 | |||
| 90 | return ''; | ||
| 91 | }; | ||
| 92 | |||
| 93 | ajax '/ajax/control/admin/users/del' => require_role setting('defanged_admin') => sub { | ||
| 94 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 95 | |||
| 96 | schema('netdisco')->txn_do(sub { | ||
| 97 | schema('netdisco')->resultset('User') | ||
| 98 | ->find({username => param('username')})->delete; | ||
| 99 | }); | ||
| 100 | |||
| 101 | return ''; | ||
| 102 | }; | ||
| 103 | |||
| 104 | ajax '/ajax/control/admin/users/update' => require_role setting('defanged_admin') => sub { | ||
| 105 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 106 | my %port_control = _port_control_fields(); | ||
| 107 | |||
| 108 | schema('netdisco')->txn_do(sub { | ||
| 109 | my $user = schema('netdisco')->resultset('User') | ||
| 110 | ->find({username => param('username')}); | ||
| 111 | return unless $user; | ||
| 112 | |||
| 113 | $user->update({ | ||
| 114 | fullname => param('fullname'), | ||
| 115 | |||
| 116 | ((param('auth_method') eq 'permanent_token') ? ( | ||
| 117 | password => undef, | ||
| 118 | ldap => \'false', radius => \'false', tacacs => \'false', | ||
| 119 | token_auth_only => \'true', | ||
| 120 | token_no_expire => (param('auth_method') eq 'permanent_token' ? \"true" : \"false"), | ||
| 121 | token_acl => (param('token_acl') || undef), | ||
| 122 | ) : ( | ||
| 123 | token_auth_only => \'false', | ||
| 124 | token_no_expire => \"false", | ||
| 125 | token_acl => undef, | ||
| 126 | ((param('password') and param('password') ne '********') | ||
| 127 | ? (password => _make_password(param('password'))) | ||
| 128 | : ()), | ||
| 129 | (param('auth_method') ? ( | ||
| 130 | (ldap => (param('auth_method') eq 'ldap' ? \'true' : \'false')), | ||
| 131 | (radius => (param('auth_method') eq 'radius' ? \'true' : \'false')), | ||
| 132 | (tacacs => (param('auth_method') eq 'tacacs' ? \'true' : \'false')), | ||
| 133 | ) : ( | ||
| 134 | ldap => \'false', | ||
| 135 | radius => \'false', | ||
| 136 | tacacs => \'false', | ||
| 137 | )), | ||
| 138 | )), | ||
| 139 | |||
| 140 | %port_control, | ||
| 141 | |||
| 142 | admin => (param('admin') ? \'true' : \'false'), | ||
| 143 | note => param('note'), | ||
| 144 | }); | ||
| 145 | }); | ||
| 146 | |||
| 147 | return ''; | ||
| 148 | }; | ||
| 149 | |||
| 150 | ajax '/ajax/control/admin/users/token' => require_role setting('defanged_admin') => sub { | ||
| 151 | send_error('Bad Request', 400) unless _sanity_ok(); | ||
| 152 | |||
| 153 | my $user = schema('netdisco')->resultset('User') | ||
| 154 | ->find({ username => param('username') }); | ||
| 155 | send_error('Not Found', 404) unless $user and $user->in_storage; | ||
| 156 | |||
| 157 | my $token; | ||
| 158 | schema('netdisco')->txn_do(sub { | ||
| 159 | $user->update({ token => random_token(), token_from => time }); | ||
| 160 | $user->discard_changes(); | ||
| 161 | $token = $user->token; | ||
| 162 | }); | ||
| 163 | |||
| 164 | header('Content-Type' => 'text/plain'); | ||
| 165 | return $token; | ||
| 166 | }; | ||
| 167 | |||
| 168 | get '/ajax/content/admin/users' => require_role admin => sub { | ||
| 169 | my @results = schema('netdisco')->resultset('User') | ||
| 170 | ->search(undef, { | ||
| 171 | '+columns' => { | ||
| 172 | created => \"to_char(creation, 'YYYY-MM-DD HH24:MI')", | ||
| 173 | last_seen => \"to_char(last_on, 'YYYY-MM-DD HH24:MI')", | ||
| 174 | token_hint => \"right(token, 8)", | ||
| 175 | }, | ||
| 176 | order_by => [qw/fullname username/] | ||
| 177 | })->hri->all; | ||
| 178 | |||
| 179 | return unless scalar @results; | ||
| 180 | |||
| 181 | my @port_control_roles = keys %{ setting('portctl_by_role') || {} }; | ||
| 182 | |||
| 183 | my @host_group_acls = | ||
| 184 | schema(vars->{'tenant'})->resultset('AccessControlListName')->host_acl_names; | ||
| 185 | |||
| 186 | if ( request->is_ajax ) { | ||
| 187 | template 'ajax/admintask/users.tt', | ||
| 188 | { results => \@results, | ||
| 189 | host_group_acls => [ uniq sort {$a cmp $b} @host_group_acls ], | ||
| 190 | port_control_roles => [ uniq sort {$a cmp $b} @port_control_roles ], | ||
| 191 | }, | ||
| 192 | { layout => undef }; | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | header( 'Content-Type' => 'text/comma-separated-values' ); | ||
| 196 | template 'ajax/admintask/users_csv.tt', | ||
| 197 | { results => \@results, }, | ||
| 198 | { layout => undef }; | ||
| 199 | } | ||
| 200 | }; | ||
| 201 | |||
| 202 | true; |