WebuserAction.class.php
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
Class WebuserAction extends CommonAction{
private $tab = 'Webuser';
public function __construct() {
parent::__construct();
$this->checkLog(); //判断用户是否已经登录
$this->assign('webuserList',$this->getPower()); //用户权限
$this->assign('ispower',C('open_power')); //是否开启交互管理
}
public function index()
{
$model = M($this->tab);
//登录账号的信息
$info = $this->_getRow($model,array('id'=>array('eq',cookie('uid'))),'id,depth');
if($info){
if(C('open_power')==='Y'){//开启同等级交互管理权限
$dep_arr=explode(',',$info['depth']);
if(count($dep_arr)>1){
array_pop($dep_arr);
$sbarr=implode(',',$dep_arr);
$where['depth'] = array('like',$sbarr.',%,%');
}else{
$where['depth'] = array('like',$info['depth'].',%');
}
}else{
$where['depth'] = array('like',$info['depth'].',%');
}
}else{ //登录账号不存在
$this->clearLog();
$this->error('账号有误!',U('Public/index'));
}
$field = array('id','username','tel','email','name','isshow','ltime','lip','addtime','updatatime');
// $where['id'] = array('neq',1);
$data = $this->_getLists($model,$where,$order,$field,12,'Pages3',array('header'=>'<li style="border:none;">共%totalRows%条记录','pages'=>'%nowPage%/%totalPages%页 </li>','prev'=>'<上一页','next'=>'下一页>','first'=>'首页','last'=>'尾页','theme'=>'%header% %pages% %prePage% %linkPage% %nextPage% %end%'));
//echo $model->getLastSql();
$auth=M('authgroupf')->getField('id,title');//用户类别名称
$this->assign('auth',$auth);
$this->assign('list',$data['list']?$data['list']:array()); //列表
$this->assign('page',$data['page']); //分页
$this->assign('p',$data['p']); //当前页数
$this->assign('page_title','用户列表');
$this->assign('empty','<tr><td colspan="9" style="padding-left:15px;font-size:14px;">暂无相关信息!</td></tr>');
$this->display();
}
public function edit()
{
$id=I('id',0,'intval');
if(!$id){
$this->error('参数有误!');
}
$Admin=D($this->tab);
$data=$Admin->where('id='.$id)->find();
if($this->isPost()){
//添加用户
if(!$Admin->create()){
$this->error($Admin->getError());
}else{
if(!$Admin->password){ //修改账号,如果没有输入密码则删除password
unset($Admin->password);
}else{
$Admin->password = md5($Admin->password);
}
$lastid=$Admin->where('id='.$id)->save();
if($lastid>0){
$this->success('系统用户修改成功',U('index'));
}else{
$this->error('系统用户修改失败');
}
}
}else{
$this->assign('data',$data);
$this->display();
}
}
public function add()
{
$Admin=D($this->tab);
if($this->isPost()){
if(cookie('uid')==1){//超级用户
if(!$_POST['s_dengji']){
$this->error('请选择用户等级');
}else{
if($_POST['s_dengji']!='2' && !$_POST['c_dengji']){
$this->error('请选择父级用户');
}
}
}
//添加用户
if(!$Admin->create()){
$this->error($Admin->getError());
}else{
$depth = $this->getDepth(I('c_dengji')); //获取depth=array('depth','pid')
$Admin->depth = $depth['depth'];
$Admin->pid = $depth['pid'];
//获取与用户等级匹配的角色
$grade = count(explode(',',$depth['depth'])); //用户等级
$group_id = $this->getGroup($grade); //根据用户等级数 获取对应的角色id
if(!$group_id){
$this->error('用户角色无法匹配,请联系系统管理员');
}
$lastid=$Admin->add();
if($lastid>0){
$this->fenpei($lastid,$group_id); //添加用户时自动为用户分配角色
$this->success('系统用户添加成功',U('index'));
}else{
$this->error('系统用户添加失败');
}
}
}else{
//获取用户等级
$grade_arr=M('Authgroupf')->where('grade!=1')->field('grade')->select();
$this->assign('grade_arr',$grade_arr);//用户等级
$this->display();
}
}
/**
* 异步获取选中级别上一级用户信息
*/
public function z_user(){
$s_dengji=I('s_dengji')-1;
$c_dengji=I('c_dengji');
$user=M($this->tab)->field('id,depth,name')->select();
if($user){
foreach($user as $k=>$v){
$djnum=count(explode(',',$v['depth']));
if($djnum == $s_dengji)
$str.='<option value="'.$v['id'].'"';
if($c_dengji == $v['id']) $str.=' selected="selected"';
$str.='>'.$v['name'].'</option>';
}
}
echo json_encode($str);
}
/**
* 添加用户时自动为用户分配角色
* @param int $uid [用户id]
* @param int $group_id [角色id]
*/
private function fenpei($uid,$group_id){
$m=M('Authgroupaccessf');
$data['uid'] = $uid;
$data['group_id'] = $group_id;
$m->data($data)->add();
}
/**
* 根据用户等级数 获取对应的角色
* @param int $grade [用户等级]
* @return int 角色id
*/
private function getGroup($grade){
$m = M('Authgroupf');
$map['grade'] = array('eq',$grade);
$id = $this->_getOne($m,$map,'id');
return $id;
}
/**
* 获取当前登录账号的depth+id
* @return array('id','depth')
*/
private function getDepth($uid=0){
$m = M($this->tab);
if($uid){
$map['id'] = array('eq',$uid);
}else{
$map['id'] = array('eq',cookie('uid'));
}
$r = $m->field('id,depth')->where($map)->find();
$back['depth'] = $r['depth'].','.$r['id'];
$back['pid'] = $r['id'];
return $back;
}
public function delete()
{
$where['id']=I('id',0,'intval');
$Admin=D($this->tab);
$data=$Admin->where($where)->find();
if(!$data){
$this->error('信息有误!');
}
if($data['id'] == cookie('uid')){
$this->error('不允许删除当前使用的用户帐号');
}elseif($data['id'] == 1){
$this->error('该账号不允许删除!');
}else{
$count=$Admin->where($where)->delete();
if($count){
$this->delAuthAccess($data['id']);
$this->success('用户删除成功',U('index',array('p'=>I('p',1,'intval'))));
}else{
$this->error('用户删除失败');
}
}
}
/**
* 删除会员时删除相应的角色分配
* @param int $uid 用户id
*/
private function delAuthAccess($uid){
if($uid){
$m = M('Authgroupaccessf');
$map['uid'] = array('eq',$uid);
$r = $m->where($map)->delete();
}
}
/**
* 修改分组开启属性
**/
public function editattr()
{
$table=$this->_post('table');
$m=M($table);
$where['id']=array('eq',$this->_post('id'));
$data[$this->_post('f')]=$this->_post('status');
$count=$m->where($where)->data($data)->save();
if($count)
{
$msg['success']=true;
$msg['msg']='状态已修改';
}
else
{
$msg['success']=false;
$msg['msg']='状态修改失败';
}
echo json_encode($msg);
}
}