Apache+SVN-让用户自行修改SVN密码

上一篇 提到了在Ubuntu下搭建Apache+SVN服务,今天写了一个简单的PHP脚本支持用户自行修改SVN密码(假设这个脚本位于/var/www-ssl/svntools/svnpass.php目录下)

 

首先,用户在修改密码之前需要先登录,而登录的验证方法和访问SVN是一样的,在/etc/apache2/mods-available/alias.conf中加入如下配置

Alias /svntools/ "/var/www-ssl/svntools/" <Directory "/var/www-ssl/svntools"> AuthType Basic AuthName "svntools repository" AuthUserFile /etc/subversion/passwd Require valid-user </Directory> 

 

接下来,需要让apache运行用户www-data有读写/etc/subversion/passwd文件的权限,上一篇 提到,我们已经把www-data加入了subversion用户组,因此我们需要更改/etc/subversion/passwd文件的用户组并给予相应的权限

sudo chown root:subversion /etc/subversion/passwd sudo chmod g+w /etc/subversion/passwd

 

最后就是实现PHP页面了,代码如下,部署以后访问一下https://hostname/svntools/svnpass.php试试吧

<? $auth_user = $_SERVER["PHP_AUTH_USER"]; $auth_pass = $_SERVER["PHP_AUTH_PW"]; ?> <html> <head> <title>Change SVN Password</title> </head> <body> <? if ($_REQUEST["action"] != "modify") { ?> <mce:script language = "javaScript"><!-- function check_and_submit(myform) { var oldpass = myform.oldpass.value; var newpass = myform.newpass.value; var repeatpass = myform.repeatpass.value; if (oldpass == "") { alert("Old password is empty!"); myform.oldpass.focus(); } else if (newpass == "") { alert("New password is empty!"); myform.newpass.focus(); } else if (repeatpass == "" || repeatpass != newpass) { alert("Repeat password is empty or not same as new password!"); myform.repeatpass.focus(); } else myform.submit(); } // --></mce:script> <form method="post"> <input type="hidden" name="action" value="modify"/> <table> <tr> <td>User Name</td> <td align="left"><?=$auth_user?></td> </tr> <tr> <td>Old Password</td> <td><input type="password" size=20 name=oldpass></td> </tr> <tr> <td>New Password</td> <td><input type="password" size=20 name=newpass></td> </tr> <tr> <td>Repeat Password</td> <td><input type="password" size=20 name=repeatpass></td> </tr> <tr> <td colspan=2> <input type="button" value="Change" onclick="check_and_submit(this.form)"> <input type="reset" value="Reset"> </td> </tr> </table> </form> <? } else { $old_pass = $_REQUEST["oldpass"]; $new_pass = $_REQUEST["newpass"]; if ($old_pass != $auth_pass) $msg = "Old password is wrong!"; else { $cmd = "htpasswd -b /etc/subversion/passwd $auth_user $new_pass"; system($cmd, $res); if ($res == 0) $msg = "Change password for [$auth_user] succeed! Please login again."; else $msg = "Change password for [$auth_user] failed! Please contact server administrator."; } ?> <mce:script language="javaScript"><!-- alert("<?=$msg?>"); window.location.href="<?=$_SERVER["PHP_SELF"]?>" // --></mce:script>} <? } ?> </body> </html>

 

你可能感兴趣的:(JavaScript,apache,SVN,server,user,subversion)