Skip to content

Commit 6a91b3f

Browse files
Merge pull request #146 from gregorybesson/develop
New feature: we log every action done by the users
2 parents 79311c0 + 8af3006 commit 6a91b3f

File tree

5 files changed

+437
-1
lines changed

5 files changed

+437
-1
lines changed

config/playgrounduser.global.php.dist

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ $playgrounduser = array(
117117
'anonymous_tracking' => false,
118118

119119
// Do we use Google ReCaptcha for registration validation ?
120-
'use_recaptcha' => false
120+
'use_recaptcha' => false,
121+
122+
// log actions from users on frontend
123+
'log_frontend_user => false,
124+
125+
// log actions from users on admin
126+
'log_admin_user => false,
121127
);
122128

123129
/**

src/Entity/UserLog.php

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<?php
2+
namespace PlaygroundUser\Entity;
3+
4+
use BjyAuthorize\Acl\HierarchicalRoleInterface;
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
7+
use Doctrine\ORM\Mapping\PrePersist;
8+
use Doctrine\ORM\Mapping\PreUpdate;
9+
use Zend\InputFilter\InputFilter;
10+
use Zend\InputFilter\Factory as InputFactory;
11+
use Zend\InputFilter\InputFilterAwareInterface;
12+
use Zend\InputFilter\InputFilterInterface;
13+
14+
/**
15+
* An entity that represents a user log.
16+
*
17+
* @ORM\Entity @HasLifecycleCallbacks
18+
* @ORM\Table(name="user_log")
19+
*
20+
*/
21+
class UserLog implements InputFilterAwareInterface
22+
{
23+
protected $inputFilter;
24+
25+
/**
26+
* @var int
27+
* @ORM\Id
28+
* @ORM\Column(type="integer")
29+
* @ORM\GeneratedValue(strategy="AUTO")
30+
*/
31+
protected $id;
32+
33+
34+
35+
/**
36+
* Get the id.
37+
*
38+
* @return int
39+
*/
40+
public function getId()
41+
{
42+
return $this->id;
43+
}
44+
45+
/**
46+
* @ORM\ManyToOne(targetEntity="PlaygroundUser\Entity\User")
47+
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", onDelete="CASCADE")
48+
**/
49+
protected $user;
50+
51+
/**
52+
* @ORM\Column(name="controller_class", type="string", length=255)
53+
*/
54+
protected $controllerClass;
55+
56+
/**
57+
* @ORM\Column(name="module_name", type="string", length=255)
58+
*/
59+
protected $moduleName;
60+
61+
/**
62+
* @ORM\Column(name="route_name", type="string", length=255)
63+
*/
64+
protected $routeName;
65+
66+
/**
67+
* @ORM\Column(name="area_name", type="string", length=255)
68+
*/
69+
protected $areaName;
70+
71+
/**
72+
* @ORM\Column(type="text")
73+
*/
74+
protected $uri;
75+
76+
/**
77+
* @ORM\Column(name="action_name", type="string", length=255)
78+
*/
79+
protected $actionName;
80+
81+
/**
82+
* @ORM\Column(name="created_at", type="datetime", nullable=true)
83+
*/
84+
protected $createdAt;
85+
86+
/**
87+
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
88+
*/
89+
protected $updatedAt;
90+
91+
/** @PrePersist */
92+
public function createChrono()
93+
{
94+
$this->createdAt = new \DateTime("now");
95+
$this->updatedAt = new \DateTime("now");
96+
}
97+
98+
/** @PreUpdate */
99+
public function updateChrono()
100+
{
101+
$this->updatedAt = new \DateTime("now");
102+
}
103+
104+
/**
105+
* Set the id.
106+
*
107+
* @param int $id
108+
*
109+
* @return void
110+
*/
111+
public function setId($id)
112+
{
113+
$this->id = (int) $id;
114+
115+
return $this;
116+
}
117+
118+
/**
119+
* @return the $user
120+
*/
121+
public function getUser()
122+
{
123+
return $this->user;
124+
}
125+
126+
/**
127+
* @param field_type $user
128+
*/
129+
public function setUser($user)
130+
{
131+
$this->user = $user;
132+
133+
return $this;
134+
}
135+
136+
public function getControllerClass()
137+
{
138+
return $this->controllerClass;
139+
}
140+
141+
public function setControllerClass($controllerClass)
142+
{
143+
$this->controllerClass = $controllerClass;
144+
145+
return $this;
146+
}
147+
148+
public function getModuleName()
149+
{
150+
return $this->moduleName;
151+
}
152+
153+
public function setModuleName($moduleName)
154+
{
155+
$this->moduleName = $moduleName;
156+
157+
return $this;
158+
}
159+
160+
public function getRouteName()
161+
{
162+
return $this->routeName;
163+
}
164+
165+
public function setRouteName($routeName)
166+
{
167+
$this->routeName = $routeName;
168+
169+
return $this;
170+
}
171+
172+
public function getAreaName()
173+
{
174+
return $this->areaName;
175+
}
176+
177+
public function setAreaName($areaName)
178+
{
179+
$this->areaName = $areaName;
180+
181+
return $this;
182+
}
183+
184+
public function getUri()
185+
{
186+
return $this->uri;
187+
}
188+
189+
public function setUri($uri)
190+
{
191+
$this->uri = $uri;
192+
193+
return $this;
194+
}
195+
196+
public function getActionName()
197+
{
198+
return $this->actionName;
199+
}
200+
201+
public function setActionName($actionName)
202+
{
203+
$this->actionName = $actionName;
204+
205+
return $this;
206+
}
207+
208+
/**
209+
* Convert the object to an array.
210+
*
211+
* @return array
212+
*/
213+
public function getArrayCopy()
214+
{
215+
$obj_vars = get_object_vars($this);
216+
217+
return $obj_vars;
218+
}
219+
220+
public function setInputFilter(InputFilterInterface $inputFilter)
221+
{
222+
throw new \Exception("Not used");
223+
}
224+
225+
public function getInputFilter()
226+
{
227+
if (!$this->inputFilter) {
228+
$inputFilter = new InputFilter();
229+
$factory = new InputFactory();
230+
231+
$this->inputFilter = $inputFilter;
232+
}
233+
234+
return $this->inputFilter;
235+
}
236+
}

src/Mapper/UserLog.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace PlaygroundUser\Mapper;
4+
5+
use Doctrine\ORM\EntityManager;
6+
use ZfcUser\Mapper\User as ZfcUserMapper;
7+
use PlaygroundUser\Options\ModuleOptions;
8+
use Zend\Hydrator\HydratorInterface;
9+
10+
class UserLog extends ZfcUserMapper
11+
{
12+
/**
13+
* @var \Doctrine\ORM\EntityManager
14+
*/
15+
protected $em;
16+
17+
/**
18+
* @var \PlaygroundUser\Options\ModuleOptions
19+
*/
20+
protected $options;
21+
22+
public function __construct(EntityManager $em, ModuleOptions $options)
23+
{
24+
$this->em = $em;
25+
$this->options = $options;
26+
}
27+
28+
public function findById($id)
29+
{
30+
$er = $this->em->getRepository($this->options->getUserEntityClass());
31+
32+
return $er->find($id);
33+
}
34+
35+
public function findOneBy($array)
36+
{
37+
$er = $this->em->getRepository($this->options->getUserEntityClass());
38+
39+
return $er->findOneBy($array);
40+
}
41+
42+
public function findAllBy($sortArray = array())
43+
{
44+
$er = $this->em->getRepository($this->options->getUserEntityClass());
45+
46+
return $er->findBy(array(), $sortArray);
47+
}
48+
49+
public function findAll()
50+
{
51+
$er = $this->em->getRepository($this->options->getUserEntityClass());
52+
53+
return $er->findAll();
54+
}
55+
56+
public function removeAll($userId)
57+
{
58+
$elements = $this->findBy(array('user_id' => $userId));
59+
foreach ($elements as $element) {
60+
$this->em->remove($element);
61+
}
62+
$this->em->flush();
63+
}
64+
65+
public function insert($entity, $tableName = null, HydratorInterface $hydrator = null)
66+
{
67+
return $this->persist($entity);
68+
}
69+
70+
public function update($entity, $where = null, $tableName = null, HydratorInterface $hydrator = null)
71+
{
72+
return $this->persist($entity);
73+
}
74+
75+
protected function persist($entity)
76+
{
77+
$this->em->persist($entity);
78+
$this->em->flush();
79+
80+
return $entity;
81+
}
82+
83+
/**
84+
* We don't delete the user, but just disable it
85+
* @param unknown_type $entity
86+
*/
87+
public function remove($entity)
88+
{
89+
$entity->setState(0);
90+
$this->em->persist($entity);
91+
$this->em->flush();
92+
}
93+
}

0 commit comments

Comments
 (0)